使用 JQuery 从列表中删除特定元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9249345/
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
Removing specific element from a list with JQuery
提问by Torben
i have a dynamic list, which looks like this:
我有一个动态列表,如下所示:
<ul>
<li class="border" id="tl_1">Text1</li>
<li class="border" id="tl_2">Text2</li>
<li class="border" id="tl_3">Text3</li>
</ul>
The list can have more items than these three.
该列表可以包含比这三个更多的项目。
When someone clicks on a specific button, i want that e.g. the "tl_2" will be removed from the list. I tried it whith these JQuery commands, but non of them were working:
当有人点击特定按钮时,我希望例如“tl_2”将从列表中删除。我用这些 JQuery 命令尝试过它,但它们都没有工作:
$('#tl_2').remove();
or
或者
$('li').find('tl_1').remove();
How can i solve this?
我该如何解决这个问题?
回答by Shadow Wizard is Ear For You
You probably have more than one element with the same ID.
您可能有多个具有相同 ID 的元素。
You don't have to use ID at all in case you want to remove them by index you can use the .eq()
method:
如果您想通过索引删除它们,您根本不必使用 ID,您可以使用以下.eq()
方法:
$("#btnRemove").click(function() {
$("#myList li").eq(1).remove();
});
This will remove the second list item each click.
这将在每次单击时删除第二个列表项。
回答by gdoron is supporting Monica
You probably had some silly mistake, it should work:
您可能犯了一些愚蠢的错误,它应该可以工作:
$('#buttonId').click(function(){
$('#tl_2').remove();
});
Note that there is no need to "find" for element by id. id is unique.
请注意,不需要按 id 为元素“查找”。id 是唯一的。
$('li').find('tl_1').remove(); // And you were missing the # anyway...
Make sure you have only one element for each id
. id is like id... You can have only one with the same value.
确保每个id
. id 就像 id... 你只能有一个具有相同值的。
Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.
每个 id 值在一个文档中只能使用一次。如果多个元素被分配了相同的 ID,则使用该 ID 的查询将只选择 DOM 中第一个匹配的元素。但是,不应依赖这种行为;具有多个元素使用相同 ID 的文档是无效的。
回答by francis94c
I think you are not showing us some part of your code as i suspect you are trying to generate that id dynamically, meaning u set it dynamically as well. you have to make sure that there is no 'space' character within the id which will likely mess thing up...
我认为您没有向我们展示您的代码的某些部分,因为我怀疑您正在尝试动态生成该 ID,这意味着您也动态设置了它。您必须确保 id 中没有“空格”字符,这可能会将事情搞砸...
$('#tl_2').remove();
Works according to jquery documentation. it worked for me.
根据 jquery 文档工作。它对我有用。
回答by PeeHaa
You must have an error somewhere else, because what you have just works.
你一定在其他地方有错误,因为你刚刚工作。
Please check your error console.
请检查您的错误控制台。
Although your second example should be:
虽然你的第二个例子应该是:
$('ul').find('#tl_2').remove(); // but this isn't really needed since we are selecting by id. So just go for the first example which is faster.