开发者

(javascript) how to i get the ID of an element i've selected?

开发者 https://www.devze.com 2022-12-12 00:01 出处:网络
let\'s say I selected a bunch开发者_C百科 of elements by class. how do i find out what the ID is of each of my returned elements?Access the id property

let's say I selected a bunch开发者_C百科 of elements by class. how do i find out what the ID is of each of my returned elements?


Access the id property

for (var i = 0; i < elements.length; ++i) {
    elements[i].id
}

or

for (var i = 0; i < elements.length; ++i) {
   elements[i].getAttribute('id');
}


Try this :

 function selectedDivs (theClass) {
                         var allHTMLTags=document.getElementsByTagName("div");

                        for (i=0; i<allHTMLTags.length; i++) {
                        //Get all tags with the specified class name.
                        if (allHTMLTags[i].className==theClass) {
                        allHTMLTags[i].getAttribute('id');
                        }
                        }
                        }
0

精彩评论

暂无评论...
验证码 换一张
取 消