简体   繁体   English

删除并附加DOM元素

[英]Remove and append DOM element

I want to add and remove the element from the DOM . 我想从DOM中添加和删除元素。 For remove I am using JQuery remove() function . 对于删除,我正在使用JQuery remove()function。 Before removing the element I am coping that element using clone() method : 在删除元素之前,我使用clone()方法处理该元素:

element = $("#list-view").clone();      
$("#list-view").remove();   

This is working perfectly . 这很正常。 But In another case I want to append the same element to the DOM again . 但是在另一种情况下,我想再次将相同的元素附加到DOM。 So I am using the element which I have cloned earlier : 所以我正在使用我先前克隆的元素:

document.getElementById("container").appendChild(element);

But nothing is happening while appending the element . 但是在追加元素时什么也没有发生。 Am I missing anything ? 我有什么想念的吗?

element is a jquery object, not a dom element so use element是一个jquery对象,而不是dom元素,因此使用

element.appendTo('#container')

Demo: Fiddle 演示: 小提琴

In your console you should be getting the error Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null. 在您的控制台中,您应该收到错误Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null.

Demo: Fiddle 演示: 小提琴

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

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