使用JQuery删除没有ID或类的父级 [英] Remove parent without an id or class using JQuery

查看:181
本文介绍了使用JQuery删除没有ID或类的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些类似于以下内容的html:

So I have some html that looks like the following:

<div class='something unknown' id='something_unknown_1'>
  <button onClick='removeParent(self)'>Remove me and my parent div</div>
</div>
<div class='something unknown' id='something_unknown_2'>
  <button onClick='removeParent(self)'>Remove me and my parent div</div>
</div>

...等等.在不知道按钮ID的情况下,我该如何精确地引用触发onClick的按钮?我想最终让我的removeParent(self)方法看起来像这样:

...and so on. How exactly would I reference the button that fired the onClick without knowing the id of the button? I would like to eventually have my removeParent(self) method look like:

buttonThatWasClicked.parent().remove();

推荐答案

我将向按钮添加一个类,以便该函数不会仅绑定到您想要的按钮上,而是仅绑定到页面上的每个按钮.

I would add a class to the buttons so the function does not get bound to every button on the page only the ones you would like it to.

<div class='something unknown' id='something unknown'>
  <button class="myClass">Remove me and my parent div</div>
</div>
<div class='something unknown' id='something unknown'>
  <button class="myClass" >Remove me and my parent div</div>
</div>

然后使用jquery类选择器绑定删除父对象的函数.

Then use the jquery class selector to bind a function that removes the parent.

$(".myClass").click(function(){
    $(this).parent().remove();
});

演示: http://jsfiddle.net/cXLqK/

这篇关于使用JQuery删除没有ID或类的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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