我正在尝试使用下面的代码创建所有 DIV 的 id 数组,
var srcs = $.map($('div'),function()
{
return this.prop('id');
});
alert(srcs);
下面是我的 HTML 代码..
<div id='d1'></div>
<div id='d2'></div>
<div id='d3'></div>
<div id='d4'></div>
这是 fiddle 链接,
http://jsfiddle.net/HcL82/
最佳答案
.map()
应用于选择器;像这样:
var ids = $('div').map(function(){
return this.id;
});
console.log(ids); // ['d1', 'd2', 'd3', 'd4']
关于jquery - 帮助我在 jQuery 中使用 map() function(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7138028/