2 回答

TA貢獻1818條經驗 獲得超3個贊
使用普通 JavaScript,可以在刪除其他任何內容之前深度克隆孫子節點。然后將其附加回父級。當然如果你想把它放在其他地方,你需要附加所需的 DOM 遍歷邏輯。(CSS部分僅用于結果的視覺驗證)
const grandson = document.querySelector('.grandson');
const father = grandson.closest('.father');
const clonedGrandson = grandson.cloneNode(true);
father.querySelector('.son').remove();
father.appendChild(clonedGrandson);
.father {
background-color: red;
padding: 20px;
}
.son {
background-color: blue;
padding: 20px;
}
.grandson {
background-color: green;
padding: 20px;
}
<div class="father">
<fieldset class="son">
<div class="grandson">
<p>Save me</p>
</div>
</fieldset>
</div>

TA貢獻1820條經驗 獲得超10個贊
你可以看看這個答案,下次嘗試使用搜索欄。
如果你只是想跳過答案。
var?cnt?=?$(".remove-just-this").contents(); $(".remove-just-this").replaceWith(cnt);
- 2 回答
- 0 關注
- 186 瀏覽
添加回答
舉報