我有一个嵌入 HTML 的内联 SVG,如下所示:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:myapp2="http://example.org/myapp2" height="200px" width="200px">
<rect id="myRect" height="100px" width="100px" fill="blue" myapp2:height="200px" xlink:href="ok"/>
<myapp:piechart xmlns:myapp="http://example.org/myapp"
title="Sales by Region">
<myapp:pieslice label="Northern Region" value="1.23"/>
</myapp:piechart>
</svg>
<script>
var rect = document.getElementById("myRect");
var rectHeight = rect.getAttribute('height'); // returns 100px
var rectHref = rect.getAttributeNS('http://www.w3.org/1999/xlink', 'href'); // returns ok
var rectMyApp2Height = rect.getAttributeNS('http://example.org/myapp2', 'height'); // returns null
var rectPiechartLen = rect.getElementsByTagNameNS('http://example.org/myapp', 'piechart').length; // returns 0
</script>
我无法从自定义命名空间 myapp 和 myapp2 读取节点的值。 为什么 rectMyApp2Height 的值为空? 为什么 rectPiechartLen 的值为 0?
最佳答案
html5不支持自定义命名空间,仅支持svg、mathml和xml。
您需要使用一些 xml mime 类型来提供文档,例如text/xml 以获得命名空间支持。
关于javascript - 如何在 HTML5 中读取内联 SVG 中的自定义命名空间节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25487846/