我想从一系列项目中获取所有ID,我怎样才能做出这一条短线:
var ids = [];
$(".post").each(function(index, element) {
ids.push($(element).attr("id"));
});
Run Code Online (Sandbox Code Playgroud)
就像是:
var ids = $(".post").map("id");
Run Code Online (Sandbox Code Playgroud)
对! .map()用于jQuery对象,或$.map用于数组和对象.jQuery版本将返回一个应用了map函数的jQuery对象,因此你必须调用.get()以获取实际的数组.
var ids = $(".post").map(function(index, element) { return element.id }).get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
556 次 |
| 最近记录: |