假设我们有一组 html 标签,比如
<div id="slide1" class="slide"></div>
<div id="slide2" class="slide"></div>
<div id="slide3" class="slide"></div>
我正在尝试在 div 标签中添加一个额外的属性,比如分别从每个标签key='post'
的哈希中。{"post" : ["0","1"], "get": ["0","2"], "search": ["2","1"]}
使它看起来像,
<div id="slide1" class="slide" key="post"></div>
<div id="slide2" class="slide" key="get"></div>
<div id="slide3" class="slide" key="search"></div>
是否可以使用javascript来做到这一点?
我想出的代码看起来像
for(key in hashArray){
var el = document.getElementsByID('slide' + (slideNumber));
el.setAttribute('key', key);
slideNumber++;
}
但我收到一个错误 document.getElementByID is not a function