jqueryを使用して、特定のクラスに関連付けられているすべてのIDをどのように見つけることができますか?
何か助けて??
これを試して
//This will give you ids of all the controls have the specified class
$('.className').each(function(){
alert(this.id);
});
jQuery の使用:
var ids = $(".class").map(function() {
return this.id.length > 0 ? this.id : null;
}).get();
id.length > 0 かどうかを確認すると、id のない要素から文字列を空にしないようにできます。
のように組み合わせて使用できます$('#id.class')
。
function getIDs(className)
{
var ids = [];
$('.' + className).each(function()
{
var id = $(this).attr('id');
if (id) ids.push(id);
});
return ids;
}
function getAllIds(className){
var results = [];
$(className).each(function(){
results.push($(this).attr('id'));
});
return results;
}