Max*_*Max 7 javascript performance jquery
我想在多个div中用Ul下的条件删除li.
<div>
<ul>
<li class="sel">.....</li>
<li class="sel">.....</li>
............
<li>.....</li>
<li>.....</li>
...........
<!-- I have some 600 li's -->
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
我有200李class='sel'
.现在我需要删除剩余的400 li.
我试图以两种方式删除,例如,
$("ul", this).each(function(){
$("li", this).each(function(){
$(this).remove();
//Also tried with -- $(this).empty().remove();
});
});
Run Code Online (Sandbox Code Playgroud)
其他方式,
$("ul", this).each(function(){
$("li[class!=sel]", this).remove(); // Also tried with 'not'
});
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我试图执行这些方式在IE中获取脚本重载错误.
请帮我解决其他删除不需要的li的方法.
注意:我不想让不想要的li保持hide()状态.
提前致谢...
如果您使用Attribute Not Equal Selector,则不需要将其包装起来.each()
- 只需像这样调用它:
$('li[class!="sel"]').remove();
Run Code Online (Sandbox Code Playgroud)
选择器 ( 'li[class!="sel"]'
) 会获取所有<li>
没有类的元素sel
,然后remove()
对该整组元素调用该方法。
编辑
如果您只想定位<ul>
包含<li class="sel"></li>
元素的,您可以像这样设置上下文:
// Find the first <li> with class="sel" and get it's parent <ul>
var $ul = $('li.sel:first').parent('ul');
// Use $ul as the context for the removal of the <li> elements
$('li[class!="sel"]', $ul).remove();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1436 次 |
最近记录: |