我有一堆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