document.createElement(" svg")instanceof SVGElement为false

时间:2014-06-12 01:01:10

标签: javascript html google-chrome dom svg

当我在chrome中尝试代码document.createElement("svg") instanceof SVGElement时,它会返回false。为什么呢?

1 个答案:

答案 0 :(得分:9)

在任何上下文中创建像<svg>这样的元素并且凭空创建一个未知元素。如果要创建SVG元素,请使用:

var a = document.createElementNS("http://www.w3.org/2000/svg", "svg");

现在,如果你比较:

var result = a instanceof SVGElement;

result将是true

请参阅此 JSFiddle

中的工作示例