开发者

jQuery get all ids on page

开发者 https://www.devze.com 2022-12-21 22:22 出处:网络
Is it possible to get an array consisting开发者_运维知识库 of all id\'s on a page with jQuery?You could do this:

Is it possible to get an array consisting开发者_运维知识库 of all id's on a page with jQuery?


You could do this:

var ids = new Array();
$('[id]').each(function() { //Get elements that have an id=
  ids.push($(this).attr("id")); //add id to array
});
//do something with ids array

One note I saw testing this, the FireBug console counts as one, if that's enabled just be aware.


var ids = $('*[id]').map(function() {
    return this.id;
}).get();

The .map() method is particularly useful for getting or setting the value of a collection of elements.

http://api.jquery.com/map/


I think this would work

var array = [];
$("*").each(function(){
    if(this.id) array.push(this.id);
});
0

精彩评论

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