jQuery jquery计算ul中的li元素->长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3158649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:56:15  来源:igfitidea点击:

jquery count li elements inside ul -> length?

jquery

提问by matt

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

如果 a内部ul有多个 -li元素,则应该发生某些事情,否则不会!

What am I doing wrong?

我究竟做错了什么?

if ( $('#menu ul').length > 1 ) {

回答by jantimon

You have to count the li elements not the ul elements:

您必须计算 li 元素而不是 ul 元素:

if ( $('#menu ul li').length > 1 ) {

If you need every UL element containing at least two LI elements, use the filter function:

如果您需要每个 UL 元素至少包含两个 LI 元素,请使用 filter 函数:

$('#menu ul').filter(function(){ return $(this).children("li").length > 1 })

You can also use that in your condition:

您也可以在您的情况下使用它:

if ( $('#menu ul').filter(function(){ return $(this).children("li").length > 1 }).length) {

回答by Jyoti Raj

The correct syntax is

正确的语法是

$('ul#menu li').length

回答by Umanda

alert( "Size: " + $( "li" ).size() );

alert( "Size: " + $( "li" ).length );

Both .size() and .length return number of item. but size() method is deprecated (JQ 1.8). .length property can use for instead of size().

.size() 和 .length 都返回项目的数量。但不推荐使用 size() 方法(JQ 1.8)。.length 属性可以用于代替 size()。

also you can use

你也可以使用

alert($("ul").children().length);

回答by naseer hussain jifri

Working jsFiddle

工作 jsFiddle

Please use .size() function instead of .length and also specify li tag in selector.

请使用 .size() 函数代替 .length 并在选择器中指定 li 标签。

Change your code like.

更改您的代码。

if ( $('#menu ul li').size() > 1 ) {

回答by Sajid Khan

Use $('ul#menu').children('li').length

$('ul#menu').children('li').length

.size() instead of .length will also work

.size() 而不是 .length 也可以

回答by Penny Liu

Another approach to count number of list elements:

计算列表元素数量的另一种方法:

var num = $("#menu").find("li").length;
if (num > 1) {
  console.log(num);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="menu">
  <li>Element 1</li>
  <li>Element 2</li>
  <li>Element 3</li>
</ul>

回答by bhuvana ganesan

Make the following change:

进行以下更改:

console.log($("#menu li").length);

回答by Kelly MacInnis

Warning: Answers above only work most of the time!

警告:以上答案仅在大多数情况下有效!

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

在 jQuery 3.3.1 版本中(其他版本未测试)

$("#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 <li> elements.

仅当您的列表项不换行时才有效。如果您的项目包含在列表中,则此代码计算占用的行数而不是 <li> 元素的数量。

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

gets the actual number of <li> elements in your list not the number of lines that are occupied.

获取列表中 <li> 元素的实际数量,而不是被占用的行数。

回答by X 47 48 - IR

If you have a dom object of the ul, use the following.

如果您有 ul 的 dom 对象,请使用以下内容。

$('#my_ul').children().length;

A simple example

一个简单的例子

window.setInterval(function() {
  let ul = $('#ul');                 // Get the ul
  let length = ul.children().length; // Count of the child nodes.

  // The show!
  ul.append('<li>Item ' + (length + 1) + '</li>');
  if (5 <= length) {
    ul.empty();
    length = -1;
  }
  $('#ul_length').text(length + 1);
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h4>Count of the child nodes: <span id='ul_length'>0</span></h4>
<ul id="ul"></ul>

回答by Biju B Adoor

alert( "Size: " + $("li").size() ); 

or

或者

alert( "Size: " + $("li").length );

You can find some examples of the .size() method here.

您可以在此处找到 .size() 方法的一些示例。