因此,在阅读MDN文档和其他几篇文章之后,我设法编写了一些代码来使用JS创建svg元素。
const _namespace = 'http://www.w3.org/2000/svg';
const _svg = document.createElementNS(_namespace,'svg');
_svg.setattributeNS(_namespace,'width',this.width);
_svg.setattributeNS(_namespace,'height',this.height);
_svg.setattributeNS(_namespace,'viewBox',`0 0 ${this.width} ${this.height}`);
this.container.appendChild(_svg);
使用下面的代码将svg元素添加到DOM中,但不考虑width,height或viewBox值。使用默认尺寸300 x 150进行渲染。
现在,通过在setattributeNS方法中将_namespace更改为null,一切似乎都可以正常工作。
我的问题是对这种行为有何逻辑解释?如果只将名称空间设置为null,为什么还要麻烦使用setattributeNS方法呢?