Javascript 在iframe中编写一些html和js,不在IE中工作:$未定义?

Javascript 在iframe中编写一些html和js,不在IE中工作:$未定义?,javascript,jquery,internet-explorer,iframe,Javascript,Jquery,Internet Explorer,Iframe,我在iframe中编写了一些html和js,在IE7/8/9中不起作用,错误消息是:$未定义 我的代码是: <!DOCTYPE html> <html> <head> <title>Demo</title> <meta charset="utf-8" /> <script type="text/javascript"> window.onload=function(){

我在iframe中编写了一些html和js,在IE7/8/9中不起作用,错误消息是:$未定义

我的代码是:

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        window.onload=function(){
            var data='<html>\
                          <head>\
                              <meta charset="utf-8">\
                              <title>Demo</title>\
                              <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"><\/script>\
                              <script type="text/javascript">\
                                    $(function(){\
                                        alert("abc");\
                                    });\
                              <\/script>\
                          <\/head>\
                          <body>\
                          </body>\
                      </html>';
            window.frames["code_result"].document.open(); 
            window.frames["code_result"].document.write(data);
            window.frames["code_result"].document.close(); 
        }
    </script>
</head>
<body>
    <iframe id="code_result" frameborder="0" class="frame_result" name="code_result"></iframe>
</body>
</html>

演示
window.onload=function(){
var数据\
\
\
演示\
\
\
$(函数(){\
警报(“abc”)\
});\
\
\
\
\
';
window.frames[“code_result”].document.open();
window.frames[“code_result”]。document.write(数据);
window.frames[“code_result”].document.close();
}
谁能告诉我为什么?谢谢

更新


此错误仅在IE78/9中显示,它在Chrome和FireFox中运行良好

您需要在运行代码之前在I框架中加载jquery。JQuery尚未加载。

添加:

 $(document).ready({
    alert('123');
 });

这不是加载I帧内容的代码。这是ie的装货单。只需将I帧脚本包装在windowonload函数中,这样它就允许jquery首先加载。在ie中测试和工作。

您的ie版本是什么?我试过你的代码,它可以在Chrome和IE9上运行。我只是把它复制并粘贴到我桌面上的一个文档中,然后在Google Chrome和IE9上运行,没有错误。你能直接访问吗?这个错误只在IE78/9上显示,它在Chrome和IE9上运行得很好FireFox@allentranks是的,它可以在Chrome和Firefox中很好地工作,我不这么认为。iframe能够从“”加载jQuery。指向jQuery的链接位于写入iframe的文本中:我在chrome中得到错误:未捕获类型错误:无法读取未定义的属性“document”