繁体   English   中英

为什么这个D3代码添加了 <p> 身体外的元素,而不是它内部?

[英]Why does this D3 code add the <p> element outside the body, instead of inside it?

我正在学习D3并且使用select运算符遇到了一个问题。

具体来说,为什么以下代码将<p>元素添加到正文之外,而不是在其中?

var pData1 = d3.select("body").select("p").data([1]).enter().append("p");

我正在使用一个完全空白的HTML文件,只需<head><body>标签进行测试。

<html lang="en">
<head>
<title><!-- Insert your title here --></title>
  <script type="text/javascript" src="d3.min.js"></script>
</head>
<body>

</body>
</html>

(这重复了Lars Kotthoff的回答中的内容,但我花时间创建了演示,所以我想我还会发帖。)

问题是,与selectAll不同, select不会为enter()选择中添加的元素重新定义父元素。

d3.select("body").select("p#a")
    .data([1])
    .enter().append("p").attr("id", "a")
    .text("This paragraph is appended to <html> (the document root) 
          because no selectAll statement reset the parent element.");

d3.selectAll("p#b")
    .data([1])
    .enter().append("p").attr("id", "b")
    .text("This paragraph is appended to <html> 
          because the selectAll statement is called directly on the root.");

d3.selectAll("body").select("p#c")
    .data([1])
    .enter().append("p").attr("id", "c")
    .text("This paragraph is also appended to <html> 
          because the last selectAll statement was called directly from the root.");

d3.select("body").selectAll("p#d")
    .data([1])
    .enter().append("p").attr("id", "d")
    .text("This paragraph is appended to <body> 
          because the selectAll statement is a sub-selection of the body selection.");

d3.selectAll("body").selectAll("p#e")
    .data([1])
    .enter().append("p").attr("id", "e")
    .text("This paragraph is also appended to <body> 
          because the final selectAll statement is a sub-selection of the body.");

http://fiddle.jshell.net/eLF4H/

select语句之后使用输入链是不常见的(与selectAll相比),因为如果要进行数据连接,通常会选择多个元素。 但是, 如果要创建元素(如果它不存在)或者更新它,则有两个选项:

  • 使用selectAll语句后跟数据连接

     var pdata1 = d3.select("body").selectAll("p#data") //select element if it exists .data([dataObject]); //join to the current data pdata1.enter().append("p").attr("id", "data"); //create element if required pdata1.text(function(d){return d.textValue;}); //set or update the element based on the data 
  • 如果需要,使用if语句创建元素,并使用.datum()绑定数据

     var pdata1 = d3.select("p#data") //select element if it exists if ( pdata1.empty() ) { pdata1 = d3.select("body").append("p").attr("id", "data"); //create element if required } pdata1.datum(dataObject) //note that you don't need to put the data into an array .text(function(d){return d.textValue;}); //set or update the element based on the data 

正如评论中所建议的那样,你要做的就是使用.selectAll() 之间的差.select().selectAll().select()返回元件,同时.selectAll()返回由祖先分组元素 然后,这将根据元素的附加内容影响任何后续的.data()操作。 文档

selectAll分组也会影响后续进入的占位符节点。 因此,要在添加进入节点时指定父节点,请使用select后跟selectAll:

d3.select( “身体”)。全选( “分区”)

没有p元素可供选择,所以我建议在输入数据之前附加p元素:

var pData1 = d3.select("body").append("p")

pData1.data([1])...

做任何你需要做的事情.data,虽然没有必要追加另一个p

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM