2012-07-14 93 views

回答

1

试试这个

Live Demo

//This will give you ids of all the controls have the specified class 
$('.className').each(function(){ 
    alert(this.id); 
}); 
+0

感谢阿迪尔......我只是觉得打开我的眼睛的jQuery simpliciy的世界...... – manoj 2012-07-14 07:07:17

+0

你,欢迎@manoj – Adil 2012-07-14 07:08:08

0

你可以同时使用它们像$('#id.class')

0
function getIDs(className) 
{ 
    var ids = []; 

    $('.' + className).each(function() 
    { 
     var id = $(this).attr('id'); 
     if (id) ids.push(id); 
    }); 
    return ids; 
} 
3

使用指定的类循环所有元素,并将它们的ID存储在数组中。见jQuery .each

var ids = []; 
$('.class').each(function() { 
    if($(this).attr('id')) 
     ids.push($(this).attr('id')); 
}); 
+0

感谢,,就像魔术.. – manoj 2012-07-14 06:57:12

+0

@ manoj没问题,很乐意帮忙:) – nbrooks 2012-07-14 06:58:55

0
function getAllIds(className){ 
     var results = []; 
     $(className).each(function(){ 
       results.push($(this).attr('id')); 

     }); 
     return results; 
    } 
1

使用jQuery:

var ids = $(".class").map(function() { 
    return this.id.length > 0 ? this.id : null; 
}).get(); 

检查,如果id.length> 0确保你没有从元素空字符串没有ID。

DEMO:http://jsfiddle.net/T8YuD/

+0

@VisioN调用结束时.get()的目的是什么?它不会改变我能说的任何事情。你能解释一下吗? – Ehryk 2012-07-14 11:05:38

+0

'get()'返回一个基本的JavaScript数组,而不是jQuery-wrapped数组。阅读[文档](http://api.jquery.com/map/)中的更多信息。你也可以在DEMO中看到不同之处。 – VisioN 2012-07-14 14:23:05

相关问题