簡體   English   中英

jQuery Remove不能完全刪除元素

[英]Jquery Remove doesn't entirely remove the element

根據Jquery文檔,.remove將刪除父標記內的元素和嵌套的html元素。

使用Jquery示例

<div class="container">
<div class="hello">Hello</div>
<div class="goodbye">Goodbye</div>
</div>

$( ".hello" ).remove();

會給

<div class="container">
<div class="goodbye">Goodbye</div>
</div>

但是,我可能做錯了,但是我需要使用jquery移動元素。

如果我執行以下操作:

<div class="container">
<div class="hello">Hello</div>
<div class="goodbye">Goodbye</div>
<textarea></textarea>
</div>
<div class="test"></div>

var container = $(".container")[0].outerHTML;
$(".container").remove();
$(".test").append(container);

這將復制包含父元素的容器,刪除html元素,然后將.test附加數據。 如果我然后嘗試像這樣在textarea之后添加元素:

$("textarea").after("<p>abc123</p>");

不是將段落應用於剩余的文本區域,而是將其應用於原始文本區域和新文本區域。

有誰知道這是由於我執行此操作的方法還是有解決方法?

預先感謝您的寶貴時間。

append()移動元素,而您根本無需remove()

$(".test").append($(".container"));

暫無
暫無

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

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