5

我正在尝试计算 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>

不管ol下面有多少里,我总是得到数字2。

知道怎么回事..?

4

3 回答 3

13

尝试

$("#ol3").children().length \\ you missed () in children...

当你这样做时,$("#ol3").children.length它会返回.children()函数中的参数数量......

尝试警报$("#ol3").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(","))}

这两个参数在哪里df是......这就是为什么当你发出警报时你总是在你的代码中得到 2..

于 2010-05-31T02:11:28.540 回答
3

尝试children()代替children.

对于腰带和吊带的方法,请尝试children('li')

于 2010-05-31T02:11:14.353 回答
2

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

任何一个都可以,尽管 .length() 应该更快。

于 2010-05-31T05:34:31.300 回答