Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery子对象的长度_Jquery_Html_Children - Fatal编程技术网

jQuery子对象的长度

jQuery子对象的长度,jquery,html,children,Jquery,Html,Children,我试图计算OL的子元素 jQuery: $(document).ready(function(){ $("#my_select").change(function(){ alert($("#ol3").children.length); });}); HTML: <ol id="ol1"> <li class="2">Location 1-1</li> </ol

我试图计算OL的子元素

jQuery:

$(document).ready(function(){

$("#my_select").change(function(){
    alert($("#ol3").children.length);
                                     });});
HTML:

<ol id="ol1">
    <li class="2">Location 1-1</li>
</ol>

<ol id="ol2">
    <li class="15">Location 2-1</li>
    <li class="20">Location 2-2</li>
</ol>

<ol id="ol3">
    <li class="17">Location 3-1</li>
    <li class="16">Location 3-2</li>
    <li class="14">Location 3-3</li>
</ol>

位置1-1
位置2-1
位置2-2
位置3-1
位置3-2
位置3-3
我总是得到数字2,无论有多少李下的ol

知道发生了什么?

尝试
children()
而不是
children

对于皮带和吊带的方法,请尝试
儿童('li')

尝试

$("#ol3").children().length \\ you missed () in children...
执行
$(“#ol3”).children.length
操作时,它返回
.children()函数中的参数数

试着提醒孩子们,你会得到这个

function (d,f){var e=c.map(this,b,d);eb.test(a)||(f=d);if(f&&typeof f==="string")e=c.filter(f,e);e=this.length>1?c.unique(e):e;if((this.length>1||gb.test(f))&&fb.test(a))e=e.reverse();return this.pushStack(e,a,R.call(arguments).join(","))}
其中
d
f
是两个参数。。。这就是为什么当您发出警报时,代码中总是出现2。

您也可以使用.size()而不是.length()

两种方法都可以,不过.length()应该更快。

:)谢谢!那相当简单。:)谢谢!那很简单,我喜欢“腰带和吊带”这句话