在不知道其父元素的情况下删除 dom 元素?

发布于 2024-08-13 01:50:58 字数 617 浏览 6 评论 0原文

是否可以删除除 body 标签之外没有父级的 dom 元素?我知道使用像 jquery 这样的框架会很容易,但我试图坚持直接使用 javascript。

这是我发现的代码:

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小鸟爱天空丶 2024-08-20 01:50:59

您应该能够获取该元素的父级,然后从该

function removeElement(el) {
el.parentNode.removeChild(el);
}

Update

中删除该元素。您可以将其设置为 HTMLElement 上的新方法:

HTMLElement.prototype.remove = function() { this.parentNode.removeChild(this); return this; }

然后执行 el.remove()< /code> (这也将返回元素)

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

许你一世情深 2024-08-20 01:50:59
childDiv.remove();

适用于 Chrome 25.0.1364.155

请注意,这不适用于 IE11 或 Opera Mini,但所有其他浏览器都支持。

请参阅此处:caniuse 上的 childnode-remove 参考

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

孤寂小茶 2024-08-20 01:50:59

我认为你可以做类似的事情...

var child = document.getElementById(childDiv);
//var parent = document.getElementById(parentDiv);
child.parentNode.removeChild(child);

请参阅 node.parentNode 了解更多信息关于这一点。

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.

心不设防 2024-08-20 01:50:59
document.body.removeChild(child);
document.body.removeChild(child);
柠檬色的秋千 2024-08-20 01:50:59

使用 outerHTML 属性删除元素

remElement(document.getElementById('title'));
remElement(document.getElementById('alpha'));

function remElement(obj) {
obj.outerHTML="";
}

Removing element using outerHTML property

remElement(document.getElementById('title'));
remElement(document.getElementById('alpha'));

function remElement(obj) {
obj.outerHTML="";
}
墨离汐 2024-08-20 01:50:59

该函数使用 id 简单地删除一个元素:

function removeElement (id) { 
  document.getElementById(id).parentElement.removeChild(document.getElementById(id));
}

This function to simply remove an element using id:

function removeElement (id) { 
  document.getElementById(id).parentElement.removeChild(document.getElementById(id));
}
你的往事 2024-08-20 01:50:59

好吧,你基本上不需要知道父节点就可以从 DOM 中删除一个 DOM 元素,看下面的代码,看看 JavaScript 中删除节点元素的顺序是怎样的:

Element + parentNode + removeChild(Element);

如您所见,我们首先找到元素,然后使用 .parentNode,然后再次删除作为 Element 的子元素,因此我们不需要完全了解父母!

现在看看真正的代码:

var navigation = document.getElementById('navigation');
if(navigation) {
  navigation.parentNode.removeChild(navigation);
}

或者作为一个函数

function removeNode(element) {
  if(element) { //check if it's not null
    element.parentNode.removeChild(element);
  } 
} //call it like : removeNode(document.getElementById('navigation'));

jQuery还有广泛使用的remove()函数,例如:

$('#navigation').remove();

还有原生的ChildNode.remove(),它在IE和旧浏览器中不存在,但你可以对它进行 polyfill,请查看 MDN 中建议的 polyfill:

Polyfill

您可以在 Internet Explorer 9 及更高版本中填充 remove() 方法
使用以下代码:

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

如果您想了解更多信息,请访问此

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文