JQuery Selector重写

jos*_*iti 1 javascript jquery underscore.js

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

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

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

Raj*_*hal 10

ids = $("tr.selectON td[id]").map(function() { return this.id; }).get();
Run Code Online (Sandbox Code Playgroud)

文件:

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

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

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