簡體   English   中英

刪除右側的所有兄弟姐妹,但不刪除左側的所有兄弟姐妹

[英]Delete all siblings to the right but not to the left JQUERY JAVASCRIPT

我正在構建一個面包屑,並且當您深入以下文件夾時,將在DIV中添加一個按鈕:

<div id='breadcrumb-wrapper'><button id='uniqueId1'>FolderName1</button><button id='uniqueId2'>FolderName2</button><button id='uniqueId3'>FolderName3</button><button id='uniqueId4'>FolderName4</button></div>

每個按鈕都指向其修補程序,如果我使用以下命令,則我需要刪除用戶單擊按鈕時其右側的所有兄弟姐妹,而不是其左側的所有兄弟姐妹:

$('#breadcrumb-wrapper').nextAll('#uniqueId3').remove();

所有兄弟姐妹加上所有元素都遠離該節點,這不是主意。 如果我使用類似:

$('#uniqueId3').siblings().remove();

包括第一個和第二個兄弟姐妹在內的所有兄弟姐妹也都消失了,這不是我要做的事,那么我該如何實現呢?

在元素本身而不是父元素上使用nextAll

 $('#uniqueId3').nextAll().remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='breadcrumb-wrapper'> <button id='uniqueId1'>FolderName1</button> <button id='uniqueId2'>FolderName2</button> <button id='uniqueId3'>FolderName3</button> <button id='uniqueId4'>FolderName4</button> </div> 

您是否嘗試過“ 下一個兄弟姐妹選擇器(“上一個〜兄弟姐妹”)

在您的情況下$('#uniqueId3 ~ button').remove()應該可以工作。

$("#breadcrumb-wrapper button").click(function(){
  $(this).nextAll().remove();
});

使用這種結構,您可以像這樣進行操作,但是如果有更多您不想刪除的按鈕,則為要刪除的所有按鈕提供相同的類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM