javascript - 为什么此 D3 代码将 <p> 元素添加到主体外部,而不是内部?

标签 javascript jquery d3.js

我正在学习 D3,在使用 select 运算符时遇到了问题。

具体来说,为什么下面的代码要加上<p>元素在 body 外部,而不是 body 内部?

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 的回答中的内容,但我花了时间创建演示,所以我想我仍然会发布。)

问题在于 selectselectAll 不同,不会为 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 语句之后使用 enter 链是不常见的(相对于 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()绑定(bind)数据

    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
    

关于javascript - 为什么此 D3 代码将 <p> 元素添加到主体外部,而不是内部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23459834/

相关文章:

d3.js - 范围刷 dc.js d3.js

javascript - D3.js 版本 4 : How to properly set the x-axis-intervals for a histogram

javascript - angularjs:重新缩放文本框

jquery - 通过jQuery获取点击的图像src并放入另一个图像src

javascript - 如何执行一次函数?

javascript - 使用 JW Player flash 进行静音切换并回退到 HTML5 视频的链接不起作用

javascript - 无法从异步函数中获取 `count`

javascript - 扩展prototypejs的toggle()来实现效果

javascript - 如何从客户端隐藏或保护 JavaScript 代码

javascript - 使用 javascript 在嵌套的 JSON 对象中添加或删除特定节点