我有下一个标记结构:
$(function() {
$('#condition-container').on('click', '.component-delete', function() {
var component = $(this).parents('.workflow-component');
component.closest('.hr').remove(); //Delete nothing
//component.siblings('.hr').remove();//delete all hr but I need to delete only closest
component.remove();
});
});

.widget-box {
padding: 0;
-webkit-box-shadow: none;
box-shadow: none;
margin: 3px 0;
border: 1px solid #CCC;
}
.widget-box .widget-header {
padding-right: 10px !important;
padding-left: 10px !important;
}
.widget-header {
padding: 0 !important;
padding-left: 10px !important;
}
.widget-header {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
position: relative;
min-height: 38px;
background: #f7f7f7 repeat-x;
background-image: -webkit-linear-gradient(top, #fff 0, #eee 100%);
background-image: -o-linear-gradient(top, #fff 0, #eee 100%);
background-image: linear-gradient(to bottom, #fff 0, #eee 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff', endColorstr='#ffeeeeee', GradientType=0);
color: #669fc7;
border-bottom: 1px solid #DDD;
padding-left: 12px;
}
.widget-body {
background-color: #FFF;
}
.widget-toolbox {
background-color: #EEE;
}
.widget-toolbox.padding-8 {
padding: 8px;
}
.widget-toolbox:last-child {
padding: 2px;
border-top: 1px solid #CCC;
}
.widget-main {
padding: 12px;
}
.hr {
display: block;
height: 0;
overflow: hidden;
font-size: 0;
border-width: 1px 0 0;
border-top: 1px solid #E3E3E3;
margin: 12px 0;
border-top-color: rgba(0, 0, 0, .11);
}
.hr-dotted,
.hr.dotted {
border-style: dotted;
}
.hr-18,
.hr18 {
margin: 18px 0;
}
.hr:nth-child(3n) {
border-color: red
}
.hr:nth-child(3n+1) {
border-color: green
}
.hr:nth-child(3n+2) {
border-color: blue
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="condition-container">
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
</div>
&#13;
问题是我想通过点击按钮Delete
移除最近的.hr
块,但在第一种情况下它不会删除任何内容,在另一种情况下它会删除所有.hr
块。
预期行为:
Delete
按钮,则必须删除第一个上方区块.hr
.hr
块。如何根据示例中的代码实现此行为?
答案 0 :(得分:2)
您需要使用.next()
在兄弟之后立即定位。方法.closest()
从自身开始遍历。
component.next('.hr').remove();
$(function() {
$('#condition-container').on('click', '.component-delete', function() {
var component = $(this).parents('.workflow-component');
component.next('.hr').remove();
component.remove();
});
});
.widget-box {
padding: 0;
-webkit-box-shadow: none;
box-shadow: none;
margin: 3px 0;
border: 1px solid #CCC;
}
.widget-box .widget-header {
padding-right: 10px !important;
padding-left: 10px !important;
}
.widget-header {
padding: 0 !important;
padding-left: 10px !important;
}
.widget-header {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
position: relative;
min-height: 38px;
background: #f7f7f7 repeat-x;
background-image: -webkit-linear-gradient(top, #fff 0, #eee 100%);
background-image: -o-linear-gradient(top, #fff 0, #eee 100%);
background-image: linear-gradient(to bottom, #fff 0, #eee 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff', endColorstr='#ffeeeeee', GradientType=0);
color: #669fc7;
border-bottom: 1px solid #DDD;
padding-left: 12px;
}
.widget-body {
background-color: #FFF;
}
.widget-toolbox {
background-color: #EEE;
}
.widget-toolbox.padding-8 {
padding: 8px;
}
.widget-toolbox:last-child {
padding: 2px;
border-top: 1px solid #CCC;
}
.widget-main {
padding: 12px;
}
.hr {
display: block;
height: 0;
overflow: hidden;
font-size: 0;
border-width: 1px 0 0;
border-top: 1px solid #E3E3E3;
margin: 12px 0;
border-top-color: rgba(0, 0, 0, .11);
}
.hr-dotted,
.hr.dotted {
border-style: dotted;
}
.hr-18,
.hr18 {
margin: 18px 0;
}
.hr:nth-child(3n) {
border-color: red
}
.hr:nth-child(3n+1) {
border-color: green
}
.hr:nth-child(3n+2) {
border-color: blue
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="condition-container">
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
<div class="hr hr-18 dotted"></div>
<div class="widget-box workflow-component">
<div class="widget-header"></div>
<div class="widget-body">
<div class="widget-main"></div>
<div class="widget-toolbox padding-8 clearfix">
<button type="button" class="btn btn-xs btn-danger component-delete"><i class="ace-icon fa fa-times"></i><span class="bigger-110">Delete</span></button>
</div>
</div>
</div>
</div>