jquery count li elements inside ul -> length?

后端 未结 10 1067
有刺的猬
有刺的猬 2020-12-29 17:42

If a ul has more than one li-element inside of it, something should happen, otherwise not!

What am I doing wrong?

if ( $(\'         


        
10条回答
  •  臣服心动
    2020-12-29 18:28

    Warning: Answers above only work most of the time!

    In jQuery version 3.3.1 (haven't tested other versions)

    $("#myList li").length; 
    

    works only if your list items don't wrap. If your items wrap in the list then this code counts the number of lines occupied not the number of

  • elements.

    $("#myList").children().length;
    

    gets the actual number of

  • elements in your list not the number of lines that are occupied.

提交回复
热议问题