gpt4 book ai didi

jquery - jquery .eq() 中的 if 语句

转载 作者:行者123 更新时间:2023-12-01 02:55:58 24 4
gpt4 key购买 nike

我想删除索引小于5的li。所以我使用.eq()。我知道可以使用 if 语句来完成,但我想在 eq() 内部使用 if 语句。

<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>


$('li').each(function (i) {
$(this).eq(function () {
if (i < 5)
return i;
else
return false;
}).remove();
});

最佳答案

您可以使用lt() (小于)获取索引小于给定值的元素。

<强> Live Demo

$('li:lt(5)').remove()

描述:选择索引小于匹配集合中索引 reference 的所有元素。 .

The index-related selectors (including this "less than" selector) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set, jQuery doc.

关于jquery - jquery .eq() 中的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932619/

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