向 HTML 标签添加自定义属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2824726/
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
Add custom attribute to HTML tags
提问by Ashwani K
I am adding custom attributes to my HTMLtags something like
我正在向我的 HTMLtags 添加自定义属性,例如
<li customeId="1">
I am to access this custom attribute in IE but in firefox, I am not able to get the values of these attributes. Any suggestion on how to access custom attribute in FireFox or any other way. I am using HTML 4 for development.
我要在 IE 中访问此自定义属性,但在 Firefox 中,我无法获取这些属性的值。关于如何在 FireFox 或任何其他方式中访问自定义属性的任何建议。我正在使用 HTML 4 进行开发。
Code to access:
访问代码:
var test = licollection[index].customeId;
Thanks Ashwani
谢谢阿什瓦尼
回答by MUS
Hopefully below code will be helpful for you.
希望下面的代码对您有所帮助。
<div id="navigation">
<ul>
<li customerId="1"></li>
<li customerId="2"></li>
<li customerId="3"></li>
</ul>
</div>
var x = document.getElementById('navigation');
if (!x) return;
var liCollections = x.getElementsByTagName('li');
for (var i=0;i<liCollections.length;i++)
alert(liCollections[i].getAttribute('customerid', 0));
It's clear enough, and you can understand it easily.
讲的很清楚,你也很容易理解。
回答by Gaurav
You can use HTML 5 custom data attribute
functionality, it may helps you
您可以使用 HTML 5custom data attribute
功能,它可能对您有所帮助
Attribute Name
属性名称
The data attribute name must be at least one character long and must be prefixed with 'data-'. It should not contain any uppercase letters.
数据属性名称的长度必须至少为 1 个字符,并且必须以'data-'为前缀。它不应包含任何大写字母。
Attribute Value
属性值
The attribute value can be any string.
属性值可以是任何字符串。
Example :-
例子 :-
<ul id="vegetable-seeds">
<li data-spacing="10cm" data-sowing-time="March to June">Carrots</li>
<li data-spacing="30cm" data-sowing-time="February to March">Celery</li>
<li data-spacing="3cm" data-sowing-time="March to September">Radishes</li>
</ul>
回答by vsync
test.getAttribute('customerid');
Did you try this?
你试过这个吗?
回答by kennytm
Try
尝试
var test = licollection[index].getAttribute("customeId");
回答by mangokun
licollection[index].getAttribute("customeId")
licollection[index].getAttribute("customeId")
参考:http: //www.java2s.com/Code/JavaScriptReference/Javascript-Methods/getAttributeSyntaxParametersandNote.htm