【问题标题】:d3js add tag inside another tagd3js在另一个标签内添加标签
【发布时间】:2012-12-03 15:41:30
【问题描述】:

我想像这样在 html 中创建一个图例:

<li><span style="background-color:#n;"></span> Label</li>

但是输出是:

<li>Label <span style="background-color:#n;"></span></li>

这是我的代码:

d3.select("#myid").selectAll("li")
.data(dataset)
.enter().append("li")
.text(function(d) {return d;})
.insert("span")
.attr('style', function(d, i) {return "background-color:" + color});

有什么建议吗?谢谢

【问题讨论】:

    标签: javascript d3.js data-visualization


    【解决方案1】:

    只需在文本前插入 span:

    d3.select("#myid").selectAll("li")
    .data(dataset)
    .enter()
    .append("li")
    .text(function(d) {return d;})
    .insert("span", ":first-child")
    .attr('style', function(d, i) {return "background-color:" + color});
    

    【讨论】:

    【解决方案2】:

    好的,最后我用 css float 修复了@Asad 代码的左端。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2020-03-04
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多