gpt4 book ai didi

jquery - 删除选择器不起作用

转载 作者:行者123 更新时间:2023-12-03 23:01:29 25 4
gpt4 key购买 nike

为什么当我将 :odd 选择器传递给 remove(selector) 函数时,它不起作用?根据documentation它应该过滤已经选择的一组项目,在本例中是 li 的。

<ul id='list1'>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<ul id='list2'>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<小时/>
$(document).ready(function() {
$('#list1').children(':odd').remove(); // works as expected
$('#list2').children().remove(':odd'); // does not work
});
<小时/>

结果

-One
-Three

-One
-Two
-Three
-Four

最佳答案

这实际上是一个错误:http://bugs.jquery.com/ticket/13721

并于 17 天前在 jQuery 2.0 中修复.

问题在于它正在检查每个元素是否与选择器匹配:

for element in matched_elements:
if element matches the selector:
remove element

:odd 仅在一组匹配元素的上下文中起作用,因此这些元素都不是奇数。同样,如果将其更改为 :even,所有这些都将被删除。

关于jquery - 删除选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com