Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用d3.js删除svg文本元素_Javascript_Svg_D3.js - Fatal编程技术网

Javascript 使用d3.js删除svg文本元素

Javascript 使用d3.js删除svg文本元素,javascript,svg,d3.js,Javascript,Svg,D3.js,我可以用一个平滑的tween来更改文本的值,但是每次我清空一个svg并创建一个新的svg时。这最终会用空SVG加载DOM。我怎样才能摆脱它们 D3: 您可以删除特定的元素,执行d3。选择(“#idOfElement”)。删除()。这里不需要.exit(),因为没有数据被匹配和绑定。您需要d3。选择(“#meantext”)。删除()@larskothoff我试过了。它仍然没有从DOM中提取空的svg。您将svg附加到哪里?添加到id为“defecit”的svg中。然后,我将svg附加一个id为m

我可以用一个平滑的tween来更改文本的值,但是每次我清空一个svg并创建一个新的svg时。这最终会用空SVG加载DOM。我怎样才能摆脱它们

D3:


您可以删除特定的元素,执行
d3。选择(“#idOfElement”)。删除()
。这里不需要
.exit()
,因为没有数据被匹配和绑定。

您需要
d3。选择(“#meantext”)。删除()
@larskothoff我试过了。它仍然没有从DOM中提取空的svg。您将svg附加到哪里?添加到id为“defecit”的svg中。然后,我将svg附加一个id为meantext的文本元素。每当init函数运行时(当soemone按下按钮时),DOM中就会出现一个新的文本元素。要删除
文本
元素还是SVG?在任何情况下,您需要做的是
d3.选择(“#idOfElement”).remove()
var vis = d3.select("#visualisation"),
        commasFormatter = '$' + d3.format(",.0f"),
        numVis = d3.select("#defecit"),
        defTotal = d3.select("#defense");


    function init () {
            //Initialize mean text
            numVis.append("text")
                .attr("id", "meantext")
                .attr("y", 40)
                .attr("x", 118)
                .style("font-size", "26px")
                .style("font-family","Arial, Helvetica, sans-serif")
                .style('font-weight', 'bold')
                .style('text-color', '#525252');

            defTotal.append("text")
                .attr("id", "defense")
                .attr("y", 0)
                .attr("x", 0)
                .style("font-size", "16px")
                .style("font-family","Arial, Helvetica, sans-serif")
                .style('font-weight', 'bold')
                .style('text-color', '#525252');

            d3.select("#meantext")
            .transition()
            .duration(500)
            .ease('linear')
            .tween("text", function() {
                var i = d3.interpolate(this.textContent, dollars.value);
                return function(t) {
                  this.textContent = '$' + d3.format(",")(Math.round(i(t)));
                };
              })

//Doing something like this didn't work - kept giving me an error that the object array had no exit function
d3.select("meantext").exit().remove()



    }