使用JQuery从列表中删除特定元素 [英] Removing specific element from a list with JQuery

查看:106
本文介绍了使用JQuery从列表中删除特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态列表,看起来像这样:

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.

当某人单击特定按钮时,我想要例如"tl_2"将从列表中删除.我尝试了以下JQuery命令,但没有一个起作用:

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').remove();

$('li').find('tl_1').remove();

我该如何解决?

推荐答案

您可能有多个具有相同ID的元素.

You probably have more than one element with the same ID.

如果要按索引删除ID,则完全不需要使用ID,可以使用.eq()方法:

You don't have to use ID at all in case you want to remove them by index you can use the .eq() method:

$("#btnRemove").click(function() {
    $("#myList li").eq(1).remove();
});

这将删除每次单击的第二个列表项.

This will remove the second list item each click.

在线测试案例.

这篇关于使用JQuery从列表中删除特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆