Javascript 使用 jQuery 删除 <div>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7820162/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:43:51  来源:igfitidea点击:

Remove <div> using jQuery

javascriptjquery

提问by Dail

I have this HTML code:

我有这个 HTML 代码:

<div id="note_list">
  <div class="note">
    Text 1
    <a href="">X</a>
  </div>
  <div class="note">
    Text 2  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 3  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 4  
    <a href="">X</a>
  </div>
  <div class="note">
    Text 5  
    <a href="">X</a>
  </div>  
</div>

Now I would like to use jQuery to deletea <div>element AFTER the 'X' clicking, is it possible?

现在我想使用 jQuery在“X”点击后删除一个<div>元素,这可能吗?

First X closes:

第一个 X 关闭:

  <div class="note">
    Text 1
    <a href="">X</a>
  </div>

etc etc. Can I remove a divwithout using id=""?

等等等等。我可以不使用div来删除divid=""吗?

Thank you!

谢谢!

回答by Manuel van Rijn

$(".note a").click(function(e) {
    e.preventDefault();
    $(this).parent().remove();
});

or instead of remove()you could use slideUp()

或者remove()你可以使用slideUp()

回答by lonesomeday

Yes, use jQuery's traversal methods to find the correct element. In this case, you just need parent():

是的,使用 jQuery 的遍历方法来找到正确的元素。在这种情况下,您只需要parent()

$('div.note a').click(function(event) {
    event.preventDefault();
    $(this).parent().remove();
});

回答by Sedat Ba?ar

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

u can check here http://jsfiddle.net/3JCR7/1/

你可以在这里查看http://jsfiddle.net/3JCR7/1/