jQuery在页面上获取所有ID

是否有可能通过jQuery获取包含所有id的数组?

你可以这样做:

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 

我看到测试的一个注释,FireBug控制台算作一个,如果启用它只是意识到。

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

.map()方法对于获取或设置元素集合的值特别有用。

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

我认为这会奏效

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