2 回答
TA贡献1934条经验 获得超2个赞
您想要使用 jQuery 变量$(this)来选择单击的按钮。使用 parent()向上遍历DOM。使用next()获取下一个元素,或使用prev()获取从 DOM 中的该点开始的上一个元素。
我做了一个简单的例子来说明我的意思:
$('button').on('click', function(){
// Shows the button that was clicked on
// console.log($(this));
// Use parent to capture whole div
var wrapper = $(this).parent();
// Delete the previous 'deleteme', if no element is found, it does nothing
wrapper.prev('.deleteMe').remove();
// Delete first span after wrapper
wrapper.next('span').remove()
// Delete wrapper
wrapper.remove();
});
.wrapper {
border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<button id="1">Button 1</button>
<p>Things that get deleted on button 1</p>
</div>
<span>Item that also will be deleted on button 1</span>
<p class="deleteMe">Item before that button 1 doesn't have</p>
<div class="wrapper">
<button id="2">Button 2</button>
<p>Things that get deleted on button 2</p>
</div>
<span>Item that also will be deleted on button 2</span>
TA贡献1858条经验 获得超8个赞
找到解决方案:
// del field
$('body').on('click', '.spr-champs', function(event) {
var th_spr = $(this).parent();
var num_th = th_spr.parent().children(1).index($(this).parent());
if (num_th == 2)
{
if (th_spr.next('th'))
{
th_spr.next('th').remove();
}
th_spr.remove();
}
else
{
th_spr.prev('th').remove();
th_spr.remove();
}
});
1)找到它是哪一个var num_th = th_spr.parent().children(1).index($(this).parent());
2) 做一个简单的 if -> 如果它是第一个按钮:删除该字段及其运算符next,否则:删除该字段及其运算before符
- 2 回答
- 0 关注
- 108 浏览
添加回答
举报