javascript - 单击时删除父元素
问题描述
$('#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>
推荐阅读
- c++ - 如何使用 SIP 将 std::list 从 c++ 转换为 python
- r - 按名称分组并按外观排名并添加计数,同时消除每个州内前 2 名中未关联的名称(降序)?
- php - 试图在 foreach 中做一个 foreach 但它只返回 1 个值
- text - 为什么文本在游戏引擎中看起来如此糟糕?
- python - 使用 pytest 夹具下载数据一次,但每次测试使用新副本
- google-chrome - 在检查断点时如何在弹出窗口中复制对象而不必先展开所有属性?
- c++ - 是否可以编写可变参数模板构造函数来初始化类/结构的所有成员?
- javascript - 将每个新视频从 Google Drive 上传到 YouTube 的脚本
- php - 更新到 PHP 7.4 后 WordPress 插件出错
- python - 我在制作机器人时遇到错误