What are valid attributes for the DIV element in H

2020-06-01 07:29发布

What are all the valid DIV attributes?

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

标签: html
4条回答
劫难
2楼-- · 2020-06-01 07:48

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

  • class
  • dir
  • id
  • lang
  • style
  • title
  • xml:lang

Plus all the event attributes:

  • onclick
  • ondblclick
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onkeydown
  • onkeypress
  • onkeyup
查看更多
做自己的国王
3楼-- · 2020-06-01 07:52

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

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

John Resig on data- attributes

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.

查看更多
beautiful°
4楼-- · 2020-06-01 07:54

contenteditable attribute also helpful and rarely used.

<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

查看更多
啃猪蹄的小仙女
5楼-- · 2020-06-01 08:13

See W3C specs:

  • 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

for is for label and specifies the id attribute of the input element to which the label applies.

See also the HTML5 specs:

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

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

查看更多
登录 后发表回答