将处理程序绑定到具有相似 ID 的多个元素
我有很多元素,它们的 id
属性类似于 element_num
,其中 num
是元素的编号。我希望这样当我单击这些元素之一时它就会被删除。
第一个问题是如何使用选择器来查找这些元素(我想我需要类似正则表达式的东西)。
第二个问题是如何获取点击的元素id来删除它。
I have a lot of elements, the id
attributes of them are something like element_num
, where the num
is the number of element. I want it so that when I click on one of these elements it gets deleted.
The first question is how to use a selector to find these elements (I think I need something like regex here).
The second question is how to obtain the clicked element id to delete it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以绑定到所有它们并删除它们,如下所示:
*=
选择器获取具有包含该段的 id 的任何元素。理想情况下,您可以将所有这些可删除的项目包装在一个容器中,这样您就可以使用 DOM 选择来代替。*=
,.remove
You can bind to all of them and delete them like:
The
*=
selector get's any element with an id containing the segment. Ideally you would wrap all these deletable items in a container so you can use DOM selection instead.*=
,.remove
在 jQuery 中,你可以通过假设页面上有 30 个 div(带有 .removable 类)来选择元素
,并且你想删除被单击的那个,你可以这样做:
In jQuery you can select elements by
say you have 30 divs (with class .removable) on a page and you want to delete the one that is clicked on, you would do something like this:
您可以向这些元素添加一些类,这样您就可以选择所有元素并在单击事件上绑定删除操作,只需使用
$(".removable")$(.removable)
另外,如果你为此使用一个类,你可以只使用元素的 id 作为真实的 id 编号,然后你只需获取元素 id 即可获取真实的编号 id这个
$(this).attr('id')
this.id
You could add some class to these elements, so you could select all of them and bind a delete action on the click event, just using a class selector like
$(".removable")$(.removable)
Also, if you use a class for this, you could just use the id of the element for the real id number, and then you'd just get the element id to get the real number id with this
$(this).attr('id')
this.id