简体   繁体   中英

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":

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. Any ideas? thanks ahead!

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

arr.push(squareID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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