2 回答

TA貢獻1934條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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 關(guān)注
- 143 瀏覽
添加回答
舉報