JavaScript访问SVG的内部DOM [英] JavaScript accessing inner DOM of SVG

查看:78
本文介绍了JavaScript访问SVG的内部DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test.php 是使用PHP生成的SVG对象。

test.php is a SVG object that's being generated with PHP.

<object data="test.php" type="image/svg+xml" id="SVG" />
<script>
    var mySVG = document.getElementById("SVG");
    var svgDoc = mySVG.contentDocument;

svgDoc 为null。 (所以我无法通过JS访问svg的元素。)它应该工作,看看这个问题。我做错了什么?如何获取我的SVG的 contentDocument

svgDoc is null. (and so I can't access the elements of the svg via JS.) It should work, looking at this question. What am I doing wrong? How can I get the contentDocument of my SVG?

推荐答案

等待SVG加载,而不是您可以访问contentDocument:

You need to wait until the SVG is loaded and than you can access the contentDocument:

 var mySVG = document.getElementById("SVG");
 var svgDoc;
 mySVG.addEventListener("load",function() {
      svgDoc = mySVG.contentDocument;
      alert("SVG contentDocument Loaded!");
 }, false);

这篇关于JavaScript访问SVG的内部DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆