admin管理员组文章数量:1166644
Is it possible to remove a dom element that has no parent other than the body tag? I know this would be easy with a framework like jquery, but I'm trying to stick to straight javascript.
Here's the code I've found to do it otherwise:
function removeElement(parentDiv, childDiv){
if (childDiv == parentDiv) {
alert("The parent div cannot be removed.");
}
else if (document.getElementById(childDiv)) {
var child = document.getElementById(childDiv);
var parent = document.getElementById(parentDiv);
parent.removeChild(child);
}
else {
alert("Child div has already been removed or does not exist.");
return false;
}
}
Thanks!
Is it possible to remove a dom element that has no parent other than the body tag? I know this would be easy with a framework like jquery, but I'm trying to stick to straight javascript.
Here's the code I've found to do it otherwise:
function removeElement(parentDiv, childDiv){
if (childDiv == parentDiv) {
alert("The parent div cannot be removed.");
}
else if (document.getElementById(childDiv)) {
var child = document.getElementById(childDiv);
var parent = document.getElementById(parentDiv);
parent.removeChild(child);
}
else {
alert("Child div has already been removed or does not exist.");
return false;
}
}
Thanks!
Share Improve this question asked Nov 26, 2009 at 18:00 MatrymMatrym 17k35 gold badges99 silver badges141 bronze badges 5 |7 Answers
Reset to default 106You should be able to get the parent of the element, then remove the element from that
function removeElement(el) {
el.parentNode.removeChild(el);
}
Update
You can set this as a new method on the HTMLElement:
HTMLElement.prototype.remove = function() { this.parentNode.removeChild(this); return this; }
And then do el.remove()
(which will also return the element)
childDiv.remove();
works in Chrome 25.0.1364.155
Note that this does not work in IE11 or Opera Mini but is supported by all other browsers.
See here: reference to childnode-remove on caniuse
I think you can do something like...
var child = document.getElementById(childDiv);
//var parent = document.getElementById(parentDiv);
child.parentNode.removeChild(child);
See node.parentNode for more info on that.
document.body.removeChild(child);
Removing element using outerHTML
property
remElement(document.getElementById('title'));
remElement(document.getElementById('alpha'));
function remElement(obj) {
obj.outerHTML="";
}
This function to simply remove an element using id:
function removeElement (id) {
document.getElementById(id).parentElement.removeChild(document.getElementById(id));
}
OK, you basically don't need to know the parent to delete a DOM element from DOM, look at the below code, see how is the order to delete a node element in JavaScript:
Element
+ parentNode
+ removeChild(Element);
As you see we find the element first, then using .parentNode and then remove the child which is the Element again, so we don't need to know the parent at all!
So now look the real code:
var navigation = document.getElementById('navigation');
if(navigation) {
navigation.parentNode.removeChild(navigation);
}
or as a function
function removeNode(element) {
if(element) { //check if it's not null
element.parentNode.removeChild(element);
}
} //call it like : removeNode(document.getElementById('navigation'));
Also jQuery has remove() function which is widely use, like:
$('#navigation').remove();
Also there is native ChildNode.remove()
which is not in IE and old browsers, but you can polyfill it, look the suggested polyfill from MDN:
Polyfill
You can polyfill the remove() method in Internet Explorer 9 and higher with the following code:
//from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(item, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
If you like to learn more about it, visit this link on MDN.
本文标签: javascriptRemove dom element without knowing its parentStack Overflow
版权声明:本文标题:javascript - Remove dom element without knowing its parent? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736977432a1958366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
body
is a perfectly valid parent. – Crescent Fresh Commented Nov 26, 2009 at 18:19document
– Adam Hopkinson Commented Jan 13, 2014 at 9:28document
isn't an element, it's a node (document node) and therefore not classed as a element – iConnor Commented Jan 13, 2014 at 10:10