1

我到处都在做这种事情,我正在寻找最有效的(计算+语法)执行方式:

ids =[]
$('tr.selectON td').each( function() { 
    var answer_query = $(this).attr('id');
    if ( answer_query !== undefined ) { 
        ids.push( answer_query ) 
    }   
});

我可以访问 underscore.js,我怀疑这会有所帮助。

4

2 回答 2

10
ids = $("tr.selectON td[id]").map(function() { return this.id; }).get();

文件:

id获取具有属性 http://api.jquery.com/attribute-contains-prefix-selector/的元素

过滤 id 属性 http://api.jquery.com/map/

将结果转换为数组 http://api.jquery.com/get/

于 2012-11-07T19:35:49.627 回答
0

你可以让它更简单,但不能说 JQuery 性能:

ids =[]
$('tr.selectON td[id^=""]').each( function() { 
        ids.push( this.id ) 
});

函数中的“this”已经是一个dom对象,所以你可以直接访问它的id。

于 2012-11-07T19:39:59.813 回答