我想要创建一个交互式的SVG HTML页面。例如:如果我单击一个矩形,我想要显示一个新的SVG。
因为我的SVG是分开的,所以我想通过HTML-Object标签将它们包括进来.
我浏览web已经有一段时间了,但是我只是没有加入single中的单个元素来更改它们的属性或添加函数等等。
我的HTML文件:
<!DOCTYPE html>
<html>
<body>
<object data="svg-test.svg" type="image/svg+xml" id="svg" width="100%" height="100%"></object>
<script>
window.onload=function() {
// Get the Object by ID
var svgObject = document.getElementById("svg");
// Get the SVG document inside the Object tag
var svgDoc = svgObject.contentDocument;
// Get one of the SVG items by ID;
var svgItem = svgDoc.getElementById("svgID");
// Set the colour to something else
svgItem.setAttribute("fill", "lime");
};
</script>
</body>
</html>SVG-文件:
<svg width="900" height="600" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="100" y="120" width="200" height="350"
style="fill:white;stroke:black;stroke-width:2;fill-opacity:1.0;stroke-opacity:1.0"
id="svgID"/>
</svg>我没有使用contentDocument,而是尝试了getSVGDocument(),但这也不起作用。
发布于 2016-05-18 07:52:43
似乎Chrome在没有(本地)服务器设置时加载对象时有问题。
我尝试过Microsoft /IE,问题解决了。我可以访问SVG文件中的所有元素。
https://stackoverflow.com/questions/37286600
复制相似问题