Can I add custom attribute to HTML tag?

2018-12-31 14:14发布

Can I add custom attribute to HTML tag like this: <tag myAttri="myVal" />

13条回答
像晚风撩人
2楼-- · 2018-12-31 14:47

The jquery data() function allows you to associate arbitrary data with dom elements. Here's an example.

查看更多
浪荡孟婆
3楼-- · 2018-12-31 14:48

You can do something like this to extract the value you want from javascript instead of an attribute

<a href='#' class='click'>
    <span style='display:none;'>value for javascript</span>some text
</a>
查看更多
怪性笑人.
4楼-- · 2018-12-31 14:50

In HTML5: yes: use the data- attribute.

 <ul>
  <li data-animal-type="bird">Owl</li>
  <li data-animal-type="fish">Salmon</li>
  <li data-animal-type="spider">Tarantula</li>
</ul> 
查看更多
闭嘴吧你
5楼-- · 2018-12-31 14:57

Yes, you can, you did it in the question itself: <html myAttri="myVal"/>.

查看更多
浪荡孟婆
6楼-- · 2018-12-31 14:59

You can set properties from JavaScript.

document.getElementById("foo").myAttri = "myVal"
查看更多
路过你的时光
7楼-- · 2018-12-31 15:00

You can add custom attributes to your elements at will. But that will make your document invalid.

In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-.

查看更多
登录 后发表回答