简体   繁体   English

在Javascript中查找元素列表

[英]Finding a list of elements in Javascript

I've created a function that creates a new img and puts it in every tag which has the css class "painted": 我创建了一个函数,该函数创建一个新的img并将其放入具有css类“ painted”的每个标签中:

function putPiece(){
    var painted = document.querySelectorAll('.painted');

for (var i = 0, len = painted.length; i < len; i++) {
    var a = i+1
    var squareID = painted[i].id;
    painted[i].taken = 'BP'+a;

    var img = document.createElement("img");
    painted[i].appendChild(img);
    img.id = "BP"+a; //unique ID for every img
    img.src = "blackP.png"; //the image to use
    img.pstn = squareID; //the ID of the <td> it's in


}
}

Iv'e given properties to each object, as noted above. 如上所述,为每个对象赋予了属性。 Now I would like to create an array that would contain the "img.pstn" of all the "img" objects. 现在,我想创建一个包含所有“ img”对象的“ img.pstn”的数组。 Any ideas? 有任何想法吗? thanks ahead! 谢谢你!

在循环外部创建一个空数组,然后在循环底部添加:

arr.push(squareID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM