--- title: 全局属性 slug: Web/HTML/Global_attributes tags: - Attribute - HTML - Reference - Web translation_of: Web/HTML/Global_attributes ---
{{HTMLSidebar("Global_attributes")}}
全局属性是所有 HTML 元素共有的属性; 它们可以用于所有元素,即使属性可能对某些元素不起作用。
我们可以在所有的 HTML 元素上指定全局属性,甚至是在标准里没有指定的元素。这意味着任何非标准元素仍必须能够应用这些属性,即使使用这些元素意味着文档不再是 html5 兼容的。例如,虽然<foo>不是一个有效的 HTML 元素,但是 html5 兼容的浏览器隐藏了标记为<foo hidden>...<foo>的内容。
除了基本的 HTML 全局属性之外,还存在以下全局属性:
aria-*属性,用于改善可访问性。onabort, onautocomplete, onautocompleteerror, onblur, oncancel, oncanplay, oncanplaythrough, onchange, onclick, onclose, oncontextmenu, oncuechange, ondblclick, ondrag, ondragend, ondragenter, ondragexit, ondragleave, ondragover, ondragstart, ondrop, ondurationchange, onemptied, onended, onerror, onfocus, oninput, oninvalid, onkeydown, onkeypress, onkeyup, onload, onloadeddata, onloadedmetadata, onloadstart, onmousedown, onmouseenter, onmouseleave, onmousemove, onmouseout, onmouseover, onmouseup, onmousewheel, onpause, onplay, onplaying, onprogress, onratechange, onreset, onresize, onscroll, onseeked, onseeking, onselect, onshow, onsort, onstalled, onsubmit, onsuspend, ontimeupdate, ontoggle, onvolumechange, onwaiting.off or none,没有应用自动大写(所有字母都默认为小写字母)。on or sentences,每个句子的第一个字母默认为大写字母;所有其他字母都默认为小写字母。words,每个单词的第一个字母默认为大写字母;所有其他字母都默认为小写字母。characters,所有的字母都应该默认为大写。document.getElementsByClassName) 来选择和访问特定的元素。true 或者空字符串,表明元素是可被编辑的;false,表明元素不能被编辑。id ,作为该元素的上下文菜单(已经不被支持,将从所有浏览器中删除)。HTMLElement 接口来访问。 HTMLElement.dataset 属性可以访问它们。一个指示元素中文本方向的枚举属性。它的取值如下:
true, 这表明元素可能被拖动false, 这表明元素可能不会被拖动link,将创建一个指向拖动数据的链接。注意:item *属性是 WHATWG HTML Microdata feature的一部分。
itemscope属性的元素的后代,它的属性才可以与使用itemref项目相关联。它提供了元素 ID 列表(而不是itemids)以及文档中其他位置的其他属性。itemscope(通常)与{{htmlattrxref("itemtype")}}一起使用,以指定包含在关于特定项目代码块中的 HTML。 itemscope创建 Item 并定义与之关联的itemtype的范围。 itemtype是描述项及其属性上下文的词汇表(例如schema.org)的有效 URL。itemprops(项属性)的词汇表的 URL。 itemscope用于设置数据结构中按itemtype设置的词汇表的生效范围。slot属性的元素被分配给由{{HTMLElement("slot")}}元素创建的沟槽,其{{htmlattrxref("name", "slot")}}属性的值与slot属性的值匹配。true,表示如果可能,应检查元素是否存在拼写错误;false, 表示不应检查元素的拼写错误。0 表示元素应通过顺序键盘导航可聚焦和可到达,但其相对顺序由平台约定定义;tabindex,则它们的相对顺序遵循它们在文档中的相对位置。"yes",表示元素将被翻译。"no", 表示该元素不会被翻译。| 规范 | 状态 | 注释 |
|---|---|---|
| {{SpecName('HTML WHATWG', "dom.html#global-attributes", "Global attributes")}} | {{Spec2('HTML WHATWG')}} | From latest snapshot, {{SpecName('HTML5.1')}}, itemid, itemprop, itemref, itemscope, and itemtype have been added. |
| {{SpecName('HTML5.1', "dom.html#global-attributes", "Global attributes")}} | {{Spec2('HTML5.1')}} | Snapshot of {{SpecName('HTML WHATWG')}}. From {{SpecName('HTML5 W3C')}}, spellcheck, draggable, and dropzone have been added. |
| {{SpecName('HTML5 W3C', "dom.html#global-attributes", "Global attributes")}} | {{Spec2('HTML5 W3C')}} | Snapshot of {{SpecName('HTML WHATWG')}}. From {{SpecName("HTML4.01")}}, the concept of global attributes is introduced and the dir, lang, style, id, class, tabindex, accesskey, and title are now true global attributes.xml:lang which was initially part of XHTML, is now also part of HTML.hidden, data-*, contextmenu, contenteditable, and translate have been added. |
| {{SpecName('HTML4.01')}} | {{Spec2('HTML4.01')}} | There are no global attributes defined. Several attributes that will become global attributes in subsequent specifications are defined on a subset of elements.class and style are supported on all elements but {{HTMLElement("base")}}, {{HTMLElement("basefont")}}, {{HTMLElement("head")}}, {{HTMLElement("html")}}, {{HTMLElement("meta")}}, {{HTMLElement("param")}}, {{HTMLElement("script")}}, {{HTMLElement("style")}}, and {{HTMLElement("title")}}.dir is supported on all elements but {{HTMLElement("applet")}}, {{HTMLElement("base")}}, {{HTMLElement("basefont")}}, {{HTMLElement("bdo")}}, {{HTMLElement("br")}}, {{HTMLElement("frame")}}, {{HTMLElement("frameset")}}, {{HTMLElement("iframe")}}, {{HTMLElement("param")}}, and {{HTMLElement("script")}}.id is supported on all elements but {{HTMLElement("base")}}, {{HTMLElement("head")}}, {{HTMLElement("html")}}, {{HTMLElement("meta")}}, {{HTMLElement("script")}}, {{HTMLElement("style")}}, and {{HTMLElement("title")}}.lang is supported on all elements but {{HTMLElement("applet")}}, {{HTMLElement("base")}}, {{HTMLElement("basefont")}}, {{HTMLElement("br")}}, {{HTMLElement("frame")}}, {{HTMLElement("frameset")}}, {{HTMLElement("iframe")}}, {{HTMLElement("param")}}, and {{HTMLElement("script")}}.tabindex is only supported on {{HTMLElement("a")}}, {{HTMLElement("area")}}, {{HTMLElement("button")}}, {{HTMLElement("object")}}, {{HTMLElement("select")}}, and {{HTMLElement("textarea")}}.accesskey is only supported on {{HTMLElement("a")}}, {{HTMLElement("area")}}, {{HTMLElement("button")}}, {{HTMLElement("input")}}, {{HTMLElement("label")}}, {{HTMLElement("legend")}} and {{HTMLElement("textarea")}}.title is supported on all elements but {{HTMLElement("base")}}, {{HTMLElement("basefont")}}, {{HTMLElement("head")}}, {{HTMLElement("html")}}, {{HTMLElement("meta")}}, {{HTMLElement("param")}}, {{HTMLElement("script")}}, and {{HTMLElement("title")}}. |
{{Compat("html.global_attributes")}}