jquery - 帮助我在 jQuery 中使用 map() function()

标签 jquery

我正在尝试使用下面的代码创建所有 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/

相关文章:

javascript - 从加载 jQuery 的表单中检索数据并将其传递到父页面

javascript - 通过 jQuery 构建时 Div 的内容格式不同

javascript - 如何在 2 个字符之间进行字符串替换和交换以替换为

javascript - 需要 document.getElementById 的解决方法,以便循环遍历多个 ID 可能性。类(class)不工作

jQuery 事件 - 元素变得可见

javascript - 输入字段在预览页面中重复

c# - 如何中止 ASP.NET MVC 中的操作

jquery - 如何最好地使用 jQuery : with HTML or CSS for formatting

javascript - ajax - 将数据作为 json 发送到 php 服务器并接收响应

jquery - Fullcalendar 不在 Bootstrap 模式中呈现