簡體   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