简体   繁体   English

parent()。remove无法正常工作

[英]parent().remove is not working correctly

I have a problem with deleting the parent on a click on .icon-trash 我在点击.icon-trash时删除父项时遇到问题

<div class="ui-wrapper ui-draggable">
  <img id="link1" class="decor ui-resizable" src="http://i.imgur.com/nYkdOne.png">
  <div class="ui-resizable-handle ui-resizable-e" style="z-index: 90;"></div>
  <div class="ui-resizable-handle ui-resizable-s" style="z-index: 90;"></div>
  <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 90;"></div>
  <img class="icon-layer-up icon-on-img" src="" style="z-index: 1;">
  <img class="icon-layer-down icon-on-img" src="" style="z-index: 1;">
  <img class="icon-trash icon-on-img" src="" style="z-index: 1;">
  <img class="icon-copy-el icon-on-img" src="" style="z-index: 1;">
</div>

My js code is deliting only elements with a class .icon-on-img 我的js代码只定义了.icon-on-img类的元素

$(document).on('click', '.icon-trash', function() {  
  $(this).parent().remove();
});

How can I remove everything by clicking on .icon-trash element? 如何通过单击.icon-trash元素删除所有内容?

try this 尝试这个

  $(document).ready(function () {
        $(".icon-trash").on("click", function () {
            $(this).parent().remove();
        });
    });

working example here: (click on the orange box) http://jsfiddle.net/cpEff/ 这里的工作示例:(单击橙色框) http://jsfiddle.net/cpEff/

just added the class '.ui-wrapper' from your parent div to your jQuery 刚刚从父div中将类'.ui-wrapper'添加到了jQuery中

$(document).on('click', '.icon-trash', function() {  
  $(this).parent('.ui-wrapper').remove();
});

The following code should work: 下面的代码应该工作:

$('.icon-trash').on('click', function() {  
  $(this).parent().remove();
});

Use body on the event 在活动中使用身体

$('body').on('click', '.icon-trash', function() {  
    $(this).parent().remove();
 });

working jsfiddle http://jsfiddle.net/HdMNY/ 工作jsfiddle http://jsfiddle.net/HdMNY/

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

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