为什么这个D3代码会将元素添加到正文之外,而不是在其中?

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

具体来说,为什么下面的代码将

元素添加到正文之外,而不是在其中?

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

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

   <!-- Insert your title here -->      

(这重复了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  (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  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  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  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  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