admin管理员组文章数量:1325235
I want to add and remove the element from the DOM . For remove I am using JQuery remove() function . Before removing the element I am coping that element using clone() method :
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 . 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 ?
I want to add and remove the element from the DOM . For remove I am using JQuery remove() function . Before removing the element I am coping that element using clone() method :
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 . 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 ?
Share Improve this question edited Mar 26, 2014 at 11:24 rvandoni 3,4075 gold badges34 silver badges48 bronze badges asked Mar 26, 2014 at 11:18 V-XtremeV-Xtreme 7,3339 gold badges40 silver badges80 bronze badges 1-
1
You could simplify your logic using
.detach()
api.jquery./detach Be aware than using.detach()
data will be kept, as e.g, handlers bound to element or children. This is usually preferred/more suitable method – A. Wolff Commented Mar 26, 2014 at 11:33
1 Answer
Reset to default 11element
is a jquery object, not a dom element so use
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.
Demo: Fiddle
本文标签: javascriptRemove and append DOM elementStack Overflow
版权声明:本文标题:javascript - Remove and append DOM element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742162910a2425287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论