在 Firefox 和 IE 中,SVG <embed>
(SVG) 文档在 $(document ).ready()
时被检索被称为。
在 Chrome 中,getSVGDocument
当 $(document ).ready()
时返回 null叫做。 (虽然它似乎在大约 7 毫秒后找到它,如 setTimeout
所示。)
为什么 Chrome 找不到已加载的 <embed>
$(document ).ready()
时刻的 SVG 文档, 但 Firefox 和 IE 呢?
(我不想使用 setTimeout(7ms)
只是为了等待 Chrome catch 来!因为那是......蹩脚的。)
下面的代码简单代码显示了场景:Firefox 中的 RETURNS SVGDocument + Chrome 中的 IE RETURNS NULL(除非对 getSVG()
的调用延迟了 7 毫秒!)。
注意:此代码需要在 localhost
上运行带有 Chrome 的服务器;这是一个单独的 Chrome 问题。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
getSVG = function () {
var el = document.getElementById("embedId");
SVGDoc = el.getSVGDocument();
console.log(SVGDoc); // returns null in Chrome
}
$(document).ready(function () {
getSVG();
//setTimeout("getSVG()", 7); // this works, showing that Chrome is NOT "ready"
});
</script>
</head>
<body>
<embed id="embedId" src="man.svg" type="image/svg+xml" width="50" height="50"/>
</body>
</html>
最佳答案
试试这个
$(window).load(function(){
console.log($('#embedId')[0].getSVGDocument());
});
另一种可能的解决方案:
$(function(){
var a = $('#embedId')[0];
a.addEventListener("load",function(){
//do stuff with 'a'
},false);
});
关于javascript - 在 Chrome 中,触发 $(document).ready() 时不会加载 <embed> 资源。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13013795/