如何检查嵌入的 SVG 文档是否已加载到 html 页面中? [英] How to check if an embedded SVG document is loaded in an html page?
问题描述
我需要编辑(使用 javascript)嵌入在 html 页面中的 SVG 文档.
I need to edit (using javascript) an SVG document embedded in an html page.
加载 SVG 后,我可以访问 SVG 的 dom 及其元素.但是我无法知道 SVG dom 是否已准备好,因此我无法在加载 html 页面时对 SVG 执行默认操作.
When the SVG is loaded, I can access the dom of the SVG and its elements. But I am not able to know if the SVG dom is ready or not, so I cant' perform default actions on the SVG when the html page is loaded.
要访问 SVG dom,我使用以下代码:
To access the SVG dom, I use this code:
var svg = document.getElementById("chart").getSVGDocument();
其中chart"是嵌入元素的 id.
where "chart" is the id of the embed element.
如果我在 html 文档准备好时尝试访问 SVG,可以这样:
If I try to access the SVG when the html document is ready, in this way:
jQuery(document).ready( function() {
var svg = document.getElementById("chart").getSVGDocument();
...
svg 始终为空.我只需要知道它什么时候不为空,这样我就可以开始操作它了.你知道有没有办法做到这一点?
svg is always null. I just need to know when it is not null, so I can start manipulate it. Do you know if there is a way to do it?
推荐答案
你可以不时尝试投票.
function checkReady() {
var svg = document.getElementById("chart").getSVGDocument();
if (svg == null) {
setTimeout("checkReady()", 300);
} else {
...
}
}
这篇关于如何检查嵌入的 SVG 文档是否已加载到 html 页面中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!