繁体   English   中英

在JQuery中,将一组节点的数据属性放入数组的最佳方法是什么?

[英]In JQuery, what's the best way to get a data attribute of a set of nodes into an array?

在JQuery中,将一组节点的数据属性放入数组的最佳方法是什么?

我使这种语法工作:

var ids = $("a.some_class").map(function(index, item) {return item.getAttribute('data-id');});

有没有更简单的语法呢?

谢谢

尝试

.data(键)

var ids = $("a.some_class").map(function() {
    return $(this).data('id'); //or return this.getAttribute('data-id');
}).get();

没有什么比您的map方法更容易的了,但是带有get data方法将有助于以跨浏览器的方式完成操作并返回纯JavaScript数组:

var data = $('a.some_class').map(function() {
    return $(this).data('id');
}).get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM