1

这是我的代码:

var svg = d3.select("body").append("svg");
console.log(svg.attr);
svg.attr("width", width + margin.left + margin.right);
svg.attr("height", height + margin.top + margin.bottom);
svg.append("g");
svg.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

console.log(svg) 输出:

function (t,e){if(arguments.length<2){if("string"==typeof t){var n=this.node();return t=ja.ns.qualify(t),t.local?n.getAttributeNS(t.space,t.local):n.getAttribute(t)}for(e in t)this.each(xe(e,t[e]));return this}return this.each(xe(t,e))} 

但是,当我尝试在那之后立即调用 svg.attr 方法时,我在控制台中得到了这个:

Uncaught TypeError: Cannot call method 'attr' of null

有人可以解释一下吗?显然 svg 不为空,因为我在尝试使用它之前将其登录到控制台...

在此处输入图像描述

4

0 回答 0