javascript - How to get the length of an ordered list? - Stack Overflow

Trying to convert some jQuery to javascript. Was using this for jQuery:var list = $('.list li'

Trying to convert some jQuery to javascript. Was using this for jQuery:

 var list = $('.list li'),
     listLength = list.length;

Now I'm trying it with JS, but it doesn't seem to work:

 var list = document.querySelector(".list"),
     listLi = list.getElementsByTagName("li"),
     listLength = listLi.length;

Any ideas?

Trying to convert some jQuery to javascript. Was using this for jQuery:

 var list = $('.list li'),
     listLength = list.length;

Now I'm trying it with JS, but it doesn't seem to work:

 var list = document.querySelector(".list"),
     listLi = list.getElementsByTagName("li"),
     listLength = listLi.length;

Any ideas?

Share Improve this question asked Jan 8, 2013 at 7:16 user1929705user1929705 211 silver badge8 bronze badges 3
  • 2 Works just fine for me - jsfiddle/gWvCQ – Andreas Commented Jan 8, 2013 at 7:20
  • 1 Your Vannila JS code works just fine. – Sandeep Commented Jan 8, 2013 at 7:22
  • 1 Yeah, thanks, just had a typo in the actual code. Oops! – user1929705 Commented Jan 8, 2013 at 7:25
Add a ment  | 

1 Answer 1

Reset to default 3

Your code should already work. But you can also use document.querySelectorAll() to perform a selector match which is cleaner than the two separate calls:

var listLi = document.querySelectorAll(".list li"),
    listLength = listLi.length;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619054a4636409.html

相关推荐

  • javascript - How to get the length of an ordered list? - Stack Overflow

    Trying to convert some jQuery to javascript. Was using this for jQuery:var list = $('.list li'

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信