访问HTML文件中包含的SVG文件

Mar*_*Cnu 2 html javascript svg

我在我的HTML文档中包含了一个SVG文件,<embed src="mySvg.svg" />现在我想操纵SVG中的内容.

我发现SVG不在"文档"中,而是在"窗口"中.

在我的SVG文件中,我有一个脚本:(
启动onload中的init函数)
function init(evt) {
svgDocument = evt.target.ownerDocument; svgDocument.getElementById('anIdOfMySvgFile'); // I successfully get the element of the SVG file
this.parent.document.getElementById('anIdOfMyHtmlFile'); // I successfully get the element of the HTML file
}

但是如何使用位于我的HTML文件中的脚本从SVG文件中选择一个元素?
window.document是我的HTML文件...但我不知道我的SVG文件是如何识别的!

ben*_*sch 5

在上面设置ID embed,然后用document.getElementById它来抓取它.确保通过访问SVG getSVGDocument().

<embed src="mySvg.svg" id="mySvg" />

<script type="text/javascript">
        var mySvg = document.getElementById("mySvg");
        mySvg.addEventListener("load", function() {
            var svg = mySvg.getSVGDocument();
            svg.getElementById("[insert svg element id here]");
        }
</script>
Run Code Online (Sandbox Code Playgroud)