我正在学习D3.js,并且有一些关于exit()函数的问题。请看下面的示例代码
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<h1>Hello, World!</h1>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<p>Test of selection of D3.js</p>
<script>
var p = d3.selectAll("p");
p.data([13,17,21,25])
.exit()
.remove();
p.style("font-size", function(d) { return d+"px";});
</script>
</body>
<html>
基本上,我有7个元素与p选项卡。代码提供了4个数据项,.exit().remove()删除了7-4 =3个额外的p元素。在此之后设置4个元素的大小。这是可行的。
然而,根据Mike Bosock的教程http://mbostock.github.io/d3/tutorial/circle.html,“破坏元素”部分
p.data([13,17,21,25]);
p.exit().remove();
应该也行得通。但事实并非如此。
有人知道这部分出了什么问题吗?非常感谢!
发布于 2013-07-06 08:20:37
请注意他示例中的这一部分:
var circle = svg.selectAll("circle")
.data([32, 57]);
然后:
circle.exit().remove();
在本例中,您尝试对p变量运行.exit().remove()
,而不是对其中的数据运行。在他的例子中,他根据附加到圆圈的数据调用它。
发布于 2013-07-06 08:22:34
试一试
var p = d3.selectAll("p");
p = p.data([13,17,21,25]);
p.exit().remove();
和selectAll().data().exit()
一样,selectAll()
也是选择器
https://stackoverflow.com/questions/17498366
复制相似问题