简体   繁体   English

(javascript)如何获取我选择的元素的ID?

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

let's say I selected a bunch of elements by class. 假设我按班级选择了一堆元素。 how do i find out what the ID is of each of my returned elements? 我如何找出每个返回元素的ID?

Access the id property 访问id属性

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');
                        }
                        }
                        }

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

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