说我有以下HTML:
<div class="test" data-file="1"></div>
<div class="test" data-file="2"></div>
Run Code Online (Sandbox Code Playgroud)
我想获得所有数据文件值的列表.我尝试过使用,$(.test).data("file")但这只会返回1根据jQuery文档说明它将返回的有意义...the value at the named data store for the first element in the set of matched elements.
重点first.
有没有办法告诉jQuery将所有数据值拉入数组?
当然!使用.map
var dataValues = $(".test[data-file]").map(function() {
return $(this).data("file");
}).get();
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/5muq33of/
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |