繁体   English   中英

单击时删除父元素

[英]delete parent element upon click

 $('#add').click(function () { let task = $('<div class="task"></div>'); let name_des = $('<div class="name_description"></div>'); let name = $('<div class="name"></div>'); name.append('<div id="taskName"></div>', '<div id="taskTime"></div>'); name_des.append(name, '<div class="description"></div>'); task.append('<input type="checkbox">', name_des, '<small id="delete">Delete</small>'); $('#taskCont').append(task); }); $('#delete').click(function (){ $(this).parent().remove(); });
 #taskCont{ /* background: yellow; */ display: flex; align-items: center; justify-content: center; flex-direction: column; }.task{ width: 300px; height: 50px; background: red; display: flex; align-items: center; justify-content: center; }.task:not(:first-child){ margin-top: 10px; }.task > *{ border: 1px solid white; }.task > *:not(:nth-child(2)){ width: auto; margin: 10px; }.name_description{ display: flex; align-items: center; justify-content: center; height: 20px; }.name_description *{ height: 20px; background: green; border: 2px solid yellow; }.name{ display: flex; align-items: center; justify-content: center; flex-direction: column; }.name > *{ height: 20px; border: 2px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="add">add</button> <div id="taskCont"></div>

在这段代码中,为什么删除按钮不起作用? 如何在这里删除父元素?

由于您尝试在动态创建的元素上附加事件,请尝试使用使用 jQuery 的on()委托方法 这会将事件附加到稍后添加到 DOM 的元素。

改变:

$('#delete').click(function (){

至:

$('body').on('click', '#delete', function (){

 $('#add').click(function () { let task = $('<div class="task"></div>'); let name_des = $('<div class="name_description"></div>'); let name = $('<div class="name"></div>'); name.append('<div id="taskName"></div>', '<div id="taskTime"></div>'); name_des.append(name, '<div class="description"></div>'); task.append('<input type="checkbox">', name_des, '<small id="delete">Delete</small>'); $('#taskCont').append(task); }); $('body').on('click', '#delete', function (){ $(this).parent().remove(); });
 #taskCont{ /* background: yellow; */ display: flex; align-items: center; justify-content: center; flex-direction: column; }.task{ width: 300px; height: 50px; background: red; display: flex; align-items: center; justify-content: center; }.task:not(:first-child){ margin-top: 10px; }.task > *{ border: 1px solid white; }.task > *:not(:nth-child(2)){ width: auto; margin: 10px; }.name_description{ display: flex; align-items: center; justify-content: center; height: 20px; }.name_description *{ height: 20px; background: green; border: 2px solid yellow; }.name{ display: flex; align-items: center; justify-content: center; flex-direction: column; }.name > *{ height: 20px; border: 2px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="add">add</button> <div id="taskCont"></div>

您应该动态附加点击事件。 直播可能是个不错的选择。 尝试这个。

 $(selector).live( eventName, function(){} );

暂无
暂无

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

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