[英]How to add element data attribute value inside of the element html
I can`t add data from id attribute to tag I have this:我无法将 id 属性中的数据添加到标签我有这个:
<div id="id1"></div>
<div id="id2"></div>
<div id="id3"></div>
I want this:我要这个:
<div id="id1">id1</div>
<div id="id2">id2</div>
<div id="id3">id3</div>
I wrote this in JS:我用JS写了这个:
let divs = document.getElementsByTagName('div');
for (let div of divs) {
divs.innerHTML=div.id;
}
But it doesn`t work, please help me.但它不起作用,请帮助我。
You are trying to access the innerHTML
property of an array ( divs
).您正在尝试访问数组 (
divs
) 的innerHTML
属性。 You need to access that property of your div
object, which is the current div of your loop.您需要访问
div
object 的该属性,这是循环的当前 div。
let divs = document.getElementsByTagName('div'); for (let div of divs) { // you had divs here div.innerHTML=div.id; }
<div id="id1"></div> <div id="id2"></div> <div id="id3"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.