Javascript jQuery从无序列表中删除所有列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7004059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:13:59  来源:igfitidea点击:

jQuery remove all list items from an unordered list

javascriptjqueryhtml-lists

提问by Atticus

I forgot the jQuery command that will clear all list elements from a list. I did a bit of searching, done it a bunch of times before, but just simply forgot the command.

我忘记了从列表中清除所有列表元素的 jQuery 命令。我做了一些搜索,之前做了很多次,但只是忘记了命令。

$("ul").clear()
$("ul").empty()

both didn't seem to accomplish this.. which command is it again?

两者似乎都没有完成这个......又是哪个命令?

UPDATE:
Thanks guys, I must have some syntax error on my selector.

更新:
谢谢大家,我的选择器一定有一些语法错误。

回答by Richard Dalton

$("ul").empty()works fine. Is there some other error?

$("ul").empty()工作正常。还有其他错误吗?

$('input').click(function() {
  $('ul').empty()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>test</li>
  <li>test</li>
</ul>

<input type="button" value="click me" />

http://jsfiddle.net/infernalbadger/D5ss8/

http://jsfiddle.net/infernalbadger/D5ss8/

回答by David says reinstate Monica

As noted by others, $('ul').empty()works fine, as does:

正如其他人所指出的,$('ul').empty()工作正常,如下所示:

$('ul li').remove();

JS Fiddle demo.

JS小提琴演示

回答by RoccoC5

This should work:

这应该有效:

$("ul").html('')

回答by Naor

$("ul").empty() should work and clear the childrens. you can see it here:

$("ul").empty() 应该可以工作并清除孩子们的。你可以在这里看到它:

http://jsfiddle.net/ZKFA5/

http://jsfiddle.net/ZKFA5/

回答by Nanang Rustianto

Look your class or id. Perhaps Like This $("#resi_result").html(''); This should work:

看看你的班级或身。也许像这样 $("#resi_result").html(''); 这应该有效:

回答by sparsh turkane

If you have multiple ul and want to empty specific ul then use id eg:

如果您有多个 ul 并且想要清空特定的 ul,则使用 id 例如:

<ul id="randomName">
   <li>1</li>
   <li>2</li>
   <li>3</li>
</ul>


<script>
  $('#randomName').empty();
</script>

$('input').click(function() {
  $('#randomName').empty()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul id="randomName">
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

<ul>
  <li>4</li>
  <li>5</li>
</ul>
<input type="button" value="click me" />

回答by Alexandre Lima

   var ul = document.getElementById("yourElementId");

     while (ul.firstChild)
         ul.removeChild(ul.firstChild);

回答by user330844

this worked for me with minimal code

这对我有用最少的代码

$(my_list).remove('li');

回答by Jared Farrish

An example using .remove():

使用示例.remove()

<p>Remove LI's from list</p>
<ul>
    <li>Test</li>
    <li>Test</li>
    <li>Test</li>
    <li>Test</li>
    <li>Test</li>
</ul>
<p>END</p>

setTimeout(function(){$('ul li').remove();},1000);

http://jsfiddle.net/userdude/ZAd2Y/

http://jsfiddle.net/userdude/ZAd2Y/

Also, .empty()should have worked.

此外,应该工作.empty()