Jquery将所有li的ID推入数组 [英] Jquery push all li's ID's into array
问题描述
我需要把所有的ID都放到一个数组中,我尝试过这两种方式只将第一个ID推到数组中:
var some = [];
$('ul#jdLists li')。each(function(){
some.push([$('ul#jdLists li')。attr(id)]);
});
这将返回数组中正确的项目数量,但具有第一个li
或
var some = [];
some.push([$('ul#jdLists li')。attr(id)]);
这将返回具有第一个li ID的单个项目
谢谢
这段代码: some.push([$('ul #jdLists li')。attr(id)]);
将推送由 ul发现的第一个
selector,你需要做的是获取每个 li
#jdLists li li
的ID,这可以在每个
function:
var some = [];
$('ul#jdLists li')。each(function(){
some.push($(this).attr(id));
//或
some.push(this.id);
});
I need to push all my ID's into an array, both ways I've tried this only pushes the first ID into the array:
var some = [];
$('ul#jdLists li').each(function () {
some.push([$('ul#jdLists li').attr("id")]);
});
This returns the correct number of items in the array but with the ID of the first li
or
var some = [];
some.push([$('ul#jdLists li').attr("id")]);
this returns a single item with the first li ID
thanks
This piece of code: some.push([$('ul#jdLists li').attr("id")]);
will push id of first li
found by ul#jdLists li
selector, what you need to do is to get id of each li
, which can be done inside each
function:
var some = [];
$('ul#jdLists li').each(function () {
some.push($(this).attr("id"));
// or
some.push(this.id);
});
这篇关于Jquery将所有li的ID推入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!