Jquery将所有li的ID推入数组 [英] Jquery push all li's ID's into array

查看:100
本文介绍了Jquery将所有li的ID推入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要把所有的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发现的第一个 li #jdLists li selector,你需要做的是获取每个 li 的I​​D,这可以在每个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆