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

标签 javascript jquery jquery-events

在 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/

相关文章:

jQuery 非选择器和实时绑定(bind)

javascript - 可编辑 Div 未触发 "KeyUp"事件/删除突出显示

javascript - 如何在 AngularJS 中上传多部分表单?

java - 客户端 javascript 与服务器端 Java 的速度有多快?

javascript - Chart.js 使用 foreach mvc 生成图表

javascript - 将图像文件拖放到 contenteditable div : works fine in FF, 在 Chrome 中失败得很惨

javascript - 使用标题和标题文本折叠面板

javascript - 如果值修改颜色文本

javascript - 停止传播 jQuery 委托(delegate)/实时函数不起作用

javascript - 提交时,将 html 弹出文件加载到 div 中