如何删除或替换 SVG 内容? [英] How can I remove or replace SVG content?

查看:33
本文介绍了如何删除或替换 SVG 内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段 JavaScript 代码,它创建(使用 D3.js)一个包含图表的 svg 元素.我想根据来自使用 AJAX 的 Web 服务的新数据更新图表,问题是每次我点击更新按钮时,它都会生成一个新的 svg,所以我想删除旧的或更新其内容.

I have a piece of JavaScript code which creates (using D3.js) an svg element which contains a chart. I want to update the chart based on new data coming from a web service using AJAX, the problem is that each time I click on the update button, it generates a new svg, so I want to remove the old one or update its content.

这是我创建 svg 的 JavaScript 函数的片段:

Here is a snippet from the JavaScript function where I create the svg:

var svg = d3.select("body")
        .append("svg")
        .attr("width", w)
        .attr("height", h);

如何删除旧的 svg 元素或至少替换其内容?

How can I remove the old svg element or at least replace its content?

推荐答案

解决方案如下:

d3.select("svg").remove();

这是D3.js提供的remove函数.

This is a remove function provided by D3.js.

这篇关于如何删除或替换 SVG 内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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