jQuery 你如何通过Id从<ul>中删除和<li>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15624690/
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 do you remove and <li> from a <ul> by Id?
提问by Pittfall
I have:
我有:
<ul id="ulId">
<li id="firstli">
</li>
<li id="secondli">
</li>
</ul>
and I am trying to remove secondli
.
我正在尝试删除secondli
.
This is what I have tried based on some reading.
这是我根据一些阅读尝试过的。
$('#ulId secondli').remove();
and
和
$('#ulId > secondli').remove();
but these 2 methods did not work. Any suggestions?
但这两种方法不起作用。有什么建议?
回答by j08691
Try this:
尝试这个:
$('#secondli').remove();
In jQuery, you refer to the ID of an element with a #
in front of it. See http://api.jquery.com/id-selector/
在 jQuery 中,您引用元素的 ID,并#
在它前面加上。见http://api.jquery.com/id-selector/
回答by Praveen Dabral
This will work for you
这对你有用
$('#secondli').remove();
回答by Zach Shallbetter
$('#ulId #secondli').remove();
$('#ulId #secondli').remove();
Select the container #ulId
and then the list item #secondli
. You had forgotten to add the ID tag before the second li.
选择容器#ulId
,然后选择列表项#secondli
。您忘记在第二个 li 之前添加 ID 标签。
回答by pingle60
You need to add a space between the select the li element,
您需要在 select li 元素之间添加一个空格,
$('#second li').remove(); // when referencing by id
or
或者
$(".second li").remove(); // when referencing by class
回答by nOcTIlIoN
if you want remove an <li>
with ID try :
如果你想删除一个<li>
带有 ID 的尝试:
$('#yourlist #yourli').remove()
;
$('#yourlist #yourli').remove()
;
otherwise, if you would remove an <li>
based on the content you can use :
否则,如果您要删除<li>
基于您可以使用的内容:
$('#yourlist li:contains("TextToRemove")').remove();
$('#yourlist li:contains("TextToRemove")').remove();