有没有简单的方法来清除SVG元素的内容? [英] Is there an easy way to clear an SVG element's contents?

查看:770
本文介绍了有没有简单的方法来清除SVG元素的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML中,我可以使用以下命令清除< div> 元素:

In HTML, I can clear a <div> element with this command:

div.innerHTML = "";

如果我有一个< svg> 元素?我找不到 innerHTML innerXML 甚至 innerSVG 方法。

Is there an equivalent if I have an <svg> element? I can't find an innerHTML nor innerXML or even innerSVG method.

我知道SVG DOM是XML DOM的超集,所以我知道我可以这样做:

I know the SVG DOM is a superset of the XML DOM, so I know I can do something like this:

while (svg.lastChild) {
    svg.removeChild(svg.lastChild);
}

但这既乏味又缓慢。是否有更快或更简单的方法来清除SVG元素?

But this is both tedious and slow. Is there a faster or easier way to clear an SVG element?

推荐答案

您已经给出了一个答案:您可以随时循环所有孩子并将其删除。如果您认为您有太多子节点,那么您可能希望用空的节点替换svg节点。如果你的svg节点有一些属性,你可以使用一个标签来放置所有的子节点,然后用空的节点替换它。

You already gave one answer: you can always just loop over all children and remove them. If you think that you have too many child nodes then maybe you want to replace the svg node by an empty one. If your svg node has some attributes you may use a tag where you place all the child nodes and then just replace the node with an empty one.

这篇关于有没有简单的方法来清除SVG元素的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆