0

我有如下代码在 iframe 中生成 JavaScript 代码,然后运行它。它在 FF 和 Chrome 中运行良好,但无法在 IE 中运行。它说

属性“$”的值为 null 或未定义,不是 Function 对象

任何想法?

这是演示链接:http ://www.gbtags.com/gb/demoviewer/466/empty/6b5417d7-4388-4e30-8055-0d15b10fa9a8.html.htm

<html>
<head>
<title>TEST iframe</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
  Just a iframe
  <div id="resulttab">
    <iframe id="resultiframe" frameBorder="1" width="100%" height="120"></iframe>
  </div>
    <SCRIPT>
        var iframe = $('#resultiframe')[0];

        if(iframe.contentDocument) doc = iframe.contentDocument;
        else if(iframe.contentWindow) doc = iframe.contentWindow.document;
        else doc = iframe.document;

        var js = '$(function(){console.log("hello")})';

        var result = '<html><head></head><body>TEST JqUERY<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"><\/script><script type="text/javascript">' + js + '<\/script></body></html>';

        doc.open();
        doc.writeln(result);
        doc.close();
    </SCRIPT
</BODY>
</HTML>
4

1 回答 1

2

这有效:

doc.open();
doc.writeln('<html><head></head><body>TEST JqUERY<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"><\/script>');
doc.writeln('<script type="text/javascript">' + js + '<\/script></body></html>');
doc.close();
于 2013-03-02T03:50:59.440 回答