如何查找与特定类关联的所有ID
答
试试这个
//This will give you ids of all the controls have the specified class
$('.className').each(function(){
alert(this.id);
});
答
你可以同时使用它们像$('#id.class')
。
答
function getIDs(className)
{
var ids = [];
$('.' + className).each(function()
{
var id = $(this).attr('id');
if (id) ids.push(id);
});
return ids;
}
答
使用指定的类循环所有元素,并将它们的ID存储在数组中。见jQuery .each
var ids = [];
$('.class').each(function() {
if($(this).attr('id'))
ids.push($(this).attr('id'));
});
答
function getAllIds(className){
var results = [];
$(className).each(function(){
results.push($(this).attr('id'));
});
return results;
}
答
使用jQuery:
var ids = $(".class").map(function() {
return this.id.length > 0 ? this.id : null;
}).get();
检查,如果id.length> 0确保你没有从元素空字符串没有ID。
感谢阿迪尔......我只是觉得打开我的眼睛的jQuery simpliciy的世界...... – manoj 2012-07-14 07:07:17
你,欢迎@manoj – Adil 2012-07-14 07:08:08