我有以下工作正常,但我需要对其进行调整,以便将数据文件夹级别与小于或大于的数据进行比较。
$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个列表项。
答案 0 :(得分:1)
没有任何<=
数据属性选择器,但您可以使用jQuery nextUntil
和filter
方法来检查您的后续元素data-attribute并相应地执行操作。< / p>
代码:
$("li").click(function () {
var $elem = $(this);
var level = $(this).attr("data-folder-level");
$elem.nextUntil().filter(function () {
return $(this).attr("data-folder-level") > level;
}).remove();
});
演示:http://jsfiddle.net/x0Lwvhc3/
由于您希望限制深度更改的删除,您可以使用each
,如果发现更改,请使用return false
停止。
代码:
$("li").click(function () {
var $elem = $(this);
var level = $(this).attr("data-folder-level");
$elem.nextUntil().each(function () {
if ($(this).attr("data-folder-level") <= level) {
return false;
} else {
$(this).remove();
}
})
});
答案 1 :(得分:1)
根据文档,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>