[英]D3.js exit strange behavior
我正在學習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();
應該也可以。 但事實並非如此。
有人知道那部分出了什么問題嗎? 非常感謝!
請注意他的示例的這一部分:
var circle = svg.selectAll("circle")
.data([32, 57]);
然后:
circle.exit().remove();
在您的情況下,您嘗試對p變量而不是對其中的數據運行.exit().remove()
。 在他的示例中,他在附加到圓上的數據上調用它。
嘗試
var p = d3.selectAll("p");
p = p.data([13,17,21,25]);
p.exit().remove();
selectAll()
和selectAll().data().exit()
一樣都是選擇器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.