我有一堆div.textBox,我想将数据属性应用到它。使用js在div上设置数据属性
这是我想结束了一下:
<div class="text" data-stellar-ratio="2">
我已经试过:
document.getElementByClassName('text').dataset.stellar.ratio = "2";
但它不工作...
帮助!
我有一堆div.textBox,我想将数据属性应用到它。使用js在div上设置数据属性
这是我想结束了一下:
<div class="text" data-stellar-ratio="2">
我已经试过:
document.getElementByClassName('text').dataset.stellar.ratio = "2";
但它不工作...
帮助!
document.getElementsByClassName('text')[0].setAttribute('dataset-stellar-ratio', '2')
这也做到了! – PMaly 2013-02-13 18:38:08
您拼写为getElementsByClassName
错误,您需要遍历元素集合并将stellar.ration
更改为stellarRatio
。
var eles = document.getElementsByClassName('text');
for (var x = 0; x < eles.length; x++){
eles[x].dataset.stellarRatio = "2";
}
没有为我工作。谢谢。 – PMaly 2013-02-13 18:26:49
的setAttribute做所有的浏览器不完全的工作。 http://www.w3schools.com/jsref/met_document_createattribute.asp有一个例子,肯定会奏效。
var att=document.createAttribute("whatever");
att.value="someting";
document.getElementById('mydivid').setAttributeNode(att);
尝试'element.setAttribute( '数据恒星比', '东西');' – MCL 2013-02-13 18:09:44
这应有助于:http://stackoverflow.com/questions/710275/how-to-add-update -an-attribute-to-html-element-using-javascript – tymeJV 2013-02-13 18:10:40
应该是'.dataset.stellarRatio ='2';' – Shmiddty 2013-02-13 18:12:17