jQuery 如何从父 <ul> 标签中删除所有 <li> 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6941489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one remove all <li> tags from a parent <ul> tag?
提问by Saurabh Kumar
Given:
鉴于:
<ul id="myList" class="list-group">
<li>Remove Me 1</li>
<li>Remove Me 2</li>
<li>Remove Me 3</li>
</ul>
I would like to remove all of the li children from a parent ul, so that when i do:
我想从父 ul 中删除所有 li 孩子,以便当我这样做时:
$("#myList").append("<li>New li 1</li>");
I end up with
我结束了
<ul id="myList" class="list-group">
<li>New li 1</li>
</ul>
Instead of:
代替:
<ul id="myList" class="list-group">
<li>Remove Me 1</li>
<li>Remove Me 2</li>
<li>Remove Me 3</li>
<li>New li 1</li>
</ul>
回答by Qchmqs
try this instead there is no remove i guess$('#thickBoxProductLists').empty();
试试这个,我猜没有删除$('#thickBoxProductLists').empty();
回答by Chuck Norris
If you have only li elements inside your ul
tag ,Jquery's remove()
function is all what you want.
如果您的ul
标签中只有 li 元素,那么 Jquery 的remove()
功能就是您想要的。
But if you have other text or etc, use empty()
function.
但是,如果您有其他文本或其他内容,请使用empty()
函数。