【问题标题】:How to close and just later add HTML element in D3.JS?如何关闭并稍后在 D3.JS 中添加 HTML 元素?
【发布时间】:2017-07-17 22:07:23
【问题描述】:

现在我有了下一个代码:

d3.select("#commiterStatsContainer")
        .append("div").attr("class", "chip")
        .append("img").attr("src", committers[i].avatar)
        .text(committers[i].login);

但它会将我的文本添加到 <img>...</img> 标记内。如何关闭 <img> 并稍后添加我的文本?

【问题讨论】:

    标签: javascript jquery html d3.js


    【解决方案1】:

    append 方法返回一个包含附加元素的新选择 (See documentation)。所以按照你的代码:

    d3.select("#commiterStatsContainer") // initial selection
      .append("div").attr("class", "chip") // new selection with a div
      .append("img").attr("src", committers[i].avatar) // new selection of img in that div
      .text(committers[i].login); // text for the img tag in the div.
    

    请尝试:

    var div = d3.select("#commiterStatsContainer")
      .append("div").attr("...");
    
    div.append("img").attr("....");
    div.append("p").html("....");
    

    变量div 是新创建的div 的选择,然后您可以使用div.append() 将新元素附加到该div。

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多