我正在编写一个基础架构,需要对HTML元素与SVG元素进行不同的应用.给定DOM节点,我如何判断它是SVG还是HTML元素?
您可以尝试以下内容:
if(document.getElementById("el") instanceof SVGElement) {
console.log("It's an SVG element");
}
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<g id="firstGroup">
<rect id="el" x="40" y="20" fill="blue" />
<text x="40" y="100">This is a basic SVG document!</text>
</g>
</svg>