How can I count the number of children?

后端 未结 5 1950
北荒
北荒 2020-11-27 04:57

I have a list

  • ...

I need jQuery to count the number of items in my list.

5条回答
  •  死守一世寂寞
    2020-11-27 05:27

    You don't need jQuery for this. You can use JavaScript's .childNodes.length.

    Just make sure to subtract 1 if you don't want to include the default text node (which is empty by default). Thus, you'd use the following:

    var count = elem.childNodes.length - 1;
    

提交回复
热议问题