我有以下可以正常工作的方法,但是我需要对其进行调整,以使其在小于或大于时比较数据文件夹级别。
$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>