search
HomeWeb Front-endFront-end Q&AWhat are html global attributes?

What are html global attributes?

Nov 17, 2021 pm 04:14 PM
htmlglobal properties

In HTML, global attributes refer to attributes that can be used to configure the common behavior of all elements and can be used on any element. Commonly used global attributes include: class, hidden, id, lang, style, title, dir, contenteditable, etc.

What are html global attributes?

The operating environment of this tutorial: Windows 7 system, HTML5 version, Dell G3 computer.

Local attributes: Some elements can specify their own attributes, which are called local attributes. For example, the link element has six local attributes: href, rel, hreflang, media, type, and sizes.

Global attributes: Can be used to configure behaviors common to all elements. This attribute is called a global attribute and can be used on any element.

1. Accesskey attribute

Use the accesskey attribute to set one or several shortcut keys for selecting elements on the page.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta >
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>HTML全局属性测试</title>
</head>
<body>
  <form action="">
      <p>
          Name: <input type="text" >                
      </p>
      <p>
          Password: <input type="password" >                
      </p>
      <p>
          Name: <input type="submit" >                
      </p>
  </form>
</body>
</html>

In the above example, the accesskey attribute is added to the three input elements, so that they can be used under Mac <span class="typ">Control<span class="pln"> <span class="pun"> <span class="pln"> <span class="typ">Alt<span class="pun">(<span class="typ">Option<span class="pun">)<span class="pln"> <span class="pun"> <span class="pln"> n</span></span></span></span></span></span></span></span></span></span></span>##The shortcut key accesses the Name input box. The key combinations used to trigger the accesskey mechanism vary by platform, as follows:

Browser/PlatformWindowLinuxMacFirefoxAlt Shift keyAlt Shift keyControl Alt keyInternet ExplorerAlt keyN/AN/AGoogle ChromeAlt keyAlt keyControl Alt keySafariAlt key N/AControl Alt keyOperaSame as Google ChromeSame as Google ChromeSame as Google Chrome

关于 accesskey 这个全局属性的详解,可以看一下 HTML accesskey 属性与 web 自定义键盘快捷访问

2、class 属性

class 属性用来将元素归类,这个就无需多言了。

3、contenteditable 属性

contenteditable 是 HTML5 中新增加的属性,,其用途是让用户能够修改页面上的内容。

<body>
   <!-- contenteditable属性应用 -->
   <p contenteditable="true">设置为 true 是可编辑的</p>
  </body>

如上例,p 元素的 contenteditable 属性值设置为 true 时,用户可以单击文字编辑内容。设置为 false 时禁止编辑。

4、dir 属性

dir 属性用来规定元素中文字的方向。有效值有两个:ltr(从左到右)、rtl(从右到左)。

<!-- dir属性应用 -->
<p dir="ltr">从左到右</p>
<p dir="rtl">从右到左</p>

5、draggable 属性

draggable 属性是 HTML5 支持拖放操作的方式之一,用来表示元素是否可被拖放。

6、dropzone 属性

dropzone 属性是 HTML5 支持拖放操作的方式之一,与 draggable 属性搭配使用。

7、id 属性

id 属性用来给元素分配一个唯一的标识符。这个也无需多言。需要说明的一点是,id 属性还可以用来导航到文档中的特定位置。

8、hidden 属性

hidden 是个布尔属性,表示相关元素当前不需要关注,浏览器对它的处理方式是隐藏相关元素(隐隐想起来控制一个元素的展示隐藏的时候,会自定义一个 hidden 类,然后在里面写隐藏样式),具体也可以看一下这篇介绍 HTML5 的 hidden 属性

<!-- hidden属性应用 -->
<div>这个元素将会被隐藏</div>

9、lang 属性

lang 属性用于说明元素内容使用的语言。lang 属性必须使用有效的 ISO 语音代码,使用这个属性的目的在于,让浏览器调整其表达元素内容的方式,比如在使用了文字朗读器的情况下正确发音。

<!-- lang属性应用 -->
<p>Hello - how are you?</p>

10、spellcheck 属性

spellcheck 属性用来表明浏览器是否应该对元素的内容进行拼写检查,这个属性只有用在用户可以编辑的元素上时才有意义。 spellcheck 属性可以接受的值有两个:true 和 false。至于拼写检查的实现方式则因浏览器而异。

<!-- spellcheck属性应用 -->
 <textarea>This is some lalalala text</textarea>

11、style 属性

style 属性用来直接在元素身上定义 CSS 样式,这个也不做过多描述了。

12、tabindex 属性

HTML 页面的键盘焦点可以通过按 Tab 键在各元素之间切换。用 tabindex 属性可以改变默认的转移顺序。

<!-- tabindex属性应用 -->
 <form action="">
    <label>Name: <input type="text" ></label>
    <label>City: <input type="text" ></label>
    <label>Country: <input type="text" ></label>
    <input type="submit" value="" tabindex="3">
 </form>

上面的代码实现效果是:在按 Tab 键的过程中,tabindex 为 1 的 Country 输入框第一个被选中,接着焦点会跳到 Name 输入框,最后是 submit 提交。tabindex 设置为 - 1 的元素不会在用户按下 Tab 键后被选中。

13、title 属性

title 属性提供了元素的额外信息,浏览器通常用这些东西显示工具条提示,这个在一些展示不全的文本标题也经常使用。

<!-- title属性应用 -->
<a href="https://qiqihaobenben.github.io/" title="我的个人网站">qiqihaobenben.github.io</a>

14、contextmenu 属性

自定义鼠标右键弹出菜单内容

15、data-* 属性

为元素增加自定义属性

16、translate 属性

元素和子孙节点内容是否需要本地化

推荐教程:《html视频教程

The above is the detailed content of What are html global attributes?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Benefits of React: Performance, Reusability, and MoreThe Benefits of React: Performance, Reusability, and MoreApr 15, 2025 am 12:05 AM

React’s popularity includes its performance optimization, component reuse and a rich ecosystem. 1. Performance optimization achieves efficient updates through virtual DOM and diffing mechanisms. 2. Component Reuse Reduces duplicate code by reusable components. 3. Rich ecosystem and one-way data flow enhance the development experience.

React: Creating Dynamic and Interactive User InterfacesReact: Creating Dynamic and Interactive User InterfacesApr 14, 2025 am 12:08 AM

React is the tool of choice for building dynamic and interactive user interfaces. 1) Componentization and JSX make UI splitting and reusing simple. 2) State management is implemented through the useState hook to trigger UI updates. 3) The event processing mechanism responds to user interaction and improves user experience.

React vs. Backend Frameworks: A ComparisonReact vs. Backend Frameworks: A ComparisonApr 13, 2025 am 12:06 AM

React is a front-end framework for building user interfaces; a back-end framework is used to build server-side applications. React provides componentized and efficient UI updates, and the backend framework provides a complete backend service solution. When choosing a technology stack, project requirements, team skills, and scalability should be considered.

HTML and React: The Relationship Between Markup and ComponentsHTML and React: The Relationship Between Markup and ComponentsApr 12, 2025 am 12:03 AM

The relationship between HTML and React is the core of front-end development, and they jointly build the user interface of modern web applications. 1) HTML defines the content structure and semantics, and React builds a dynamic interface through componentization. 2) React components use JSX syntax to embed HTML to achieve intelligent rendering. 3) Component life cycle manages HTML rendering and updates dynamically according to state and attributes. 4) Use components to optimize HTML structure and improve maintainability. 5) Performance optimization includes avoiding unnecessary rendering, using key attributes, and keeping the component single responsibility.

React and the Frontend: Building Interactive ExperiencesReact and the Frontend: Building Interactive ExperiencesApr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

React and the Frontend Stack: The Tools and TechnologiesReact and the Frontend Stack: The Tools and TechnologiesApr 10, 2025 am 09:34 AM

React is a JavaScript library for building user interfaces, with its core components and state management. 1) Simplify UI development through componentization and state management. 2) The working principle includes reconciliation and rendering, and optimization can be implemented through React.memo and useMemo. 3) The basic usage is to create and render components, and the advanced usage includes using Hooks and ContextAPI. 4) Common errors such as improper status update, you can use ReactDevTools to debug. 5) Performance optimization includes using React.memo, virtualization lists and CodeSplitting, and keeping code readable and maintainable is best practice.

React's Role in HTML: Enhancing User ExperienceReact's Role in HTML: Enhancing User ExperienceApr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

React Components: Creating Reusable Elements in HTMLReact Components: Creating Reusable Elements in HTMLApr 08, 2025 pm 05:53 PM

React components can be defined by functions or classes, encapsulating UI logic and accepting input data through props. 1) Define components: Use functions or classes to return React elements. 2) Rendering component: React calls render method or executes function component. 3) Multiplexing components: pass data through props to build a complex UI. The lifecycle approach of components allows logic to be executed at different stages, improving development efficiency and code maintainability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment