jQuery是否可以同时选择特定块的所有“IDS”?我知道您可以指定例如$('div'),但我想在我的对象中选择 ID。有没有简单的方法在 jQuery 中做到这一点?就像是:
$object = $('.wrapper');
$object.find(function(){
//GET ALL THE IDS..somehow?!
});
jQuery是否可以同时选择特定块的所有“IDS”?我知道您可以指定例如$('div'),但我想在我的对象中选择 ID。有没有简单的方法在 jQuery 中做到这一点?就像是:
$object = $('.wrapper');
$object.find(function(){
//GET ALL THE IDS..somehow?!
});
我对它的目的有点好奇,但你可以试试这个:
var ids = $('[id]', $object).map(function() {
return this.id;
});
它用于$object为您的选择器提供上下文,查找所述上下文中具有属性的所有元素id,然后构建值数组id。
仅供参考,结果ids变量是一个类似 jQuery 数组的对象。如果你只想要一个普通的 JS 数组,.get()在 map 函数之后添加一个:
var ids = $('[id]', $object).map(function() {
return this.id;
}).get();
// ^
$object.find('[id]').each(function(){
//this.id is your man
});