jQuery Children 的 ol 长度

发布于 2024-09-03 12:13:35 字数 736 浏览 2 评论 0原文

我正在尝试计算 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 下面有多少个 li,我总是得到数字 2。

知道发生什么事吗..?

I am trying to count the child elements of an 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>

I always get the number 2 no matter how many li are there under the ol.

Know what's going on..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£噩梦荏苒 2024-09-10 12:13:35

尝试

$("#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 。

try

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

when you do $("#ol3").children.length it returns the number of arguments in the .children() function...

try alerting $("#ol3").children and you will get this...

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(","))}

where d and f are the two arguments... that's why you are always getting 2 in your code when you alert..

甜`诱少女 2024-09-10 12:13:35

尝试使用 children() 而不是 children

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

Try children() instead of children.

For a belt-and-suspenders approach, try children('li').

花辞树 2024-09-10 12:13:35

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

,两者都可以,但 .length() 据说更快。

you can also use .size() instead of .length()

either one works, though .length() is supposedly faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文