d3js使用.append(function(){})添加元素 [英] d3js adding element using .append(function(){})

查看:1038
本文介绍了d3js使用.append(function(){})添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道为什么用d3这样做会显示一个圆圈吗?

Do you know why doing this with d3 display a circle:

node.append("circle")

但不是这样:

node.append(function () {
  return document.createElement("circle");
})

DOM结果完全相同,但是在第二种情况下看不到圆圈

The DOM result is exactly the same, but the circle is not visible in the second case

推荐答案

该元素是在错误的命名空间中创建的-您需要明确指定SVG命名空间才能正常工作,因为Javascript不会自动推断出它:

The element is created in the wrong namespace -- you need to explicitly specify the SVG namespace for this to work, as Javascript doesn't infer it automatically:

return document.createElementNS(d3.ns.prefix.svg, "circle");

这篇关于d3js使用.append(function(){})添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆