gpt4 book ai didi

javascript - jQuery nextUntil 属性小于或等于值

转载 作者:行者123 更新时间:2023-11-29 21:40:34 25 4
gpt4 key购买 nike

我有以下工作正常,但我需要对其进行调整,以便它将数据文件夹级别与小于或大于进行比较。

$elem.nextUntil( $("li[data-folder-level="+level+"]"), "li" ).remove();

我试过了...

$elem.nextUntil( $("li[data-folder-level<="+level+"]"), "li" ).remove();

但这行不通。

有什么想法吗?

编辑:

示例 html

<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">CLICK ME!!!!!!!!!!!!!!!</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="3"><a href="#">Will be removed...</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>

因此单击列表中的第 3 个将删除接下来的 4 个列表项。

最佳答案

根据文档,nextUntil需要一个选择器或一个元素作为第一个参数。

但是,从 jQuery 1.7 开始,函数似乎也可以工作了。所以你可以使用

$elem.nextUntil(function () {
return $(this).attr("data-folder-level") <= level;
}, "li").remove();

$("ol").on("click", "li", function () {
var level = +$(this).attr("data-folder-level");
$(this).nextUntil(function () {
return $(this).attr("data-folder-level") <= level;
}, "li").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">CLICK ME!!!!!!!!!!!!!!!</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="2"><a href="#">Will be removed...</a></li>
<li data-folder-level="3"><a href="#">Will be removed...</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="0"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="1"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>
<li data-folder-level="2"><a href="#">Hello world</a></li>
</ol>

关于javascript - jQuery nextUntil 属性小于或等于值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083585/

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