HTML 中 DIV 元素的有效属性是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2185519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:04:37  来源:igfitidea点击:

What are valid attributes for the DIV element in HTML?

html

提问by JasonK

What are all the valid DIVattributes?

什么是所有有效DIV属性?

Based on this link, it appears only class, id, title. Is that correct? Meaning, the forattribute and others is not valid for DIV?

根据此链接,它仅显示class, id, title。那是对的吗?意思是,for属性和其他对DIV?

回答by Sinan ünür

See W3C specs:

请参阅W3C 规范

  • id, class(document-wide identifiers)
  • lang(language information), dir(text direction)
  • title(element title)
  • style(inline style information)
  • align(alignment)
  • onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup
  • id, class(文档范围的标识符)
  • lang(语言信息), dir(文本方向)
  • title(元素标题)
  • style(内联样式信息)
  • align(结盟)
  • onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown,onkeyup

foris for labeland specifies the idattribute of the input element to which the label applies.

for用于label并指定id标签适用的输入元素的属性。

See also the HTML5 specs:

另请参阅HTML5 规范

The divelement has no special meaning at all. It represents its children. It can be used with the class, lang, and titleattributes to mark up semantics common to a group of consecutive elements.

Note:Authors are strongly encouraged to view the divelement as an element of last resort, for when no other element is suitable. Use of more appropriate elements instead of the divelement leads to better accessibility for readers and easier maintainability for authors.

div元素根本没有特殊含义。它代表它的孩子。它可以与被使用classlangtitle属性语义共同标记到一组连续的元素。

注意:强烈鼓励作者将该div元素视为最后的元素,因为当没有其他元素适合时。使用更合适的元素而不是div元素会为读者带来更好的可访问性,并为作者带来更容易的可维护性。

回答by kapa

With HTML5, besides using the global attributeswhich can be used on any element, you can also define your own attributes using the data-attributes.

在 HTML5 中,除了使用可用于任何元素的全局属性外,您还可以使用data-attributes定义自己的属性

<div class="marvellous" data-for="xy" data-another-attribute="nothing"></div>

John Resig on data-attributes

John Resig 关于data-属性

All you have to do is use the HTML5 doctype (<!DOCTYPE html>) and your page will be valid. This doctype works fine even in older browsers.

您所要做的就是使用 HTML5 doctype ( <!DOCTYPE html>) 并且您的页面将有效。即使在较旧的浏览器中,此文档类型也能正常工作。

回答by Ajay Gupta

contenteditable attribute also helpful and rarely used.

contenteditable 属性也很有帮助但很少使用。

<div contenteditable="true">Using this on runtime content can changed.</div>

This attribute is provide a way to use this like a Normal textbox. but text changed on browser side is not saved in coding.

该属性提供了一种像普通文本框一样使用它的方法。但是在浏览器端更改的文本不会保存在编码中。

FIDDLE

小提琴

回答by Guillaume Flandre

A list of attributes is available here: https://www.w3schools.com/tags/tag_div.asp

属性列表可在此处获得:https: //www.w3schools.com/tags/tag_div.asp

  • class
  • dir
  • id
  • lang
  • style
  • title
  • xml:lang
  • 班级
  • 目录
  • ID
  • 风格
  • 标题
  • xml:lang

Plus all the event attributes:

加上所有事件属性:

  • onclick
  • ondblclick
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onkeydown
  • onkeypress
  • onkeyup
  • 点击
  • ondblclick
  • 鼠标按下
  • 鼠标移动
  • 鼠标移出
  • 鼠标悬停
  • 上鼠标
  • 按键按下
  • 按键
  • 上键