简体   繁体   English

jQuery Remove不能完全删除元素

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

Based on the Jquery documentation .remove will remove the element and nested html elements inside the parent tag. 根据Jquery文档,.remove将删除父标记内的元素和嵌套的html元素。

Using the Jquery example 使用Jquery示例

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

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

will give 会给

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

However, I maybe doing this incorrectly but i need to use jquery to move an element. 但是,我可能做错了,但是我需要使用jquery移动元素。

If i do the following: 如果我执行以下操作:

<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);

This will make a copy of the container including the parent element, remove the html elements and then append .test with the data. 这将复制包含父元素的容器,删除html元素,然后将.test附加数据。 If i then try to add an element after the textarea like this: 如果我然后尝试像这样在textarea之后添加元素:

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

Instead of applying the paragraph to the textarea that is left it applies it to where the original textarea should be and also to the new textarea. 不是将段落应用于剩余的文本区域,而是将其应用于原始文本区域和新文本区域。

Does anyone know if this is due to the way i have done this or if there is a way around? 有谁知道这是由于我执行此操作的方法还是有解决方法?

Thank you in advance for your time. 预先感谢您的宝贵时间。

append()移动元素,而您根本无需remove()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM