简体   繁体   English

去掉 <div> 使用jQuery

[英]Remove <div> using jQuery

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 delete a <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 div without using id="" ? 等等。我可以在不使用id=""情况下删除div吗?

Thank you! 谢谢!

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

or instead of remove() you could use slideUp() 或者代替remove()你可以使用slideUp()

Yes, use jQuery's traversal methods to find the correct element. 是的,使用jQuery的遍历方法来查找正确的元素。 In this case, you just need parent() : 在这种情况下,您只需要parent()

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM