在Chrome中,触发$(document).ready()时,不会加载资源。 为什么?

在FF和IE中,当调用$(document ).ready()时,将检索SVG(SVG)文档。

在Chrome中,当getSVGDocument $(document ).ready()时, getSVGDocument返回null。 (虽然它似乎在大约7ms之后找到它,如setTimeout所示。)

为什么Chrome在$(document ).ready()时刻没有找到加载的 SVGdocument,但FF和IE呢?

(我不想使用setTimeout(7ms)只是等待Chrome赶上!!!因为那是……跛脚。)

下面的代码简单代码显示了场景:在Chrome中getSVG()在Chrome中IE浏览器返回NULL (除非对getSVG()的调用延迟了7ms !!!)。

注意:此代码需要在Chrome的localhost服务器上运行; 这是一个单独的Chrome问题。


       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" });       

试试这个

 $(window).load(function(){ console.log($('#embedId')[0].getSVGDocument()); }); 

另一种可能的方案

 $(function(){ var a = $('#embedId')[0]; a.addEventListener("load",function(){ //do stuff with 'a' },false); }); 

您应该使用$(window).load()而不是$(document).ready()来等待加载的嵌入,iframe和图像