javascript - 使用 document.write() 将 html 写入 iframe,但 jquery 在 IE9 中不起作用

标签 javascript jquery html internet-explorer-9

我在本地系统中存储了一个 html 页面,我尝试获取内容然后写入 iframe。

<html>
<head>
    <title></title>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <link href="styles/myStlye.css" rel="stylesheet" type="text/css" />
</head>
<body><button id="buttonClick">Click Me</button></body>
<script>
    $(document).ready(function(){
        $("#buttonClick").click(function(){
            alert("Button clicked.");
        });
    });
</script>
</html>

我使用 document.write() 将 html 代码写入 iframe,但在 IE9 中加载页面时出现错误 SCRIPT5009: '$' is undefined,但在 google chrome 和 firefox 中工作正常.

最佳答案

问题可能是 IE 在加载 jquery 之前执行了 JS 代码,尝试动态加载并监听 onload:

<html>

<head>
  <title></title>
  <link href="styles/myStlye.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <button id="buttonClick">Click Me</button>
  <script>
    var script = document.createElement("script");

    script.src = "js/jquery.min.js";

    script.onreadystatechange = function () {
      if (this.readyState == 'complete' || this.readyState == 'loaded') {
        $("#buttonClick").click(function () {
          alert("Button clicked.");
        });
      }
    };
    script.onload = function () {
      $("#buttonClick").click(function () {
        alert("Button clicked.");
      });
    };
    document.querySelector('body').appendChild(script);
  </script>
</body>

</html>

关于javascript - 使用 document.write() 将 html 写入 iframe,但 jquery 在 IE9 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019445/

相关文章:

php - JS for 和 while 循环与 jQuery ajax 在 Chrome 中锁定

javascript - 根据属性值选择元素

javascript - 遍历每个工具提示以添加不同的时间戳

javascript - 在 jQuery 生成的 div 上使用 Masonry.js

javascript - html中的水平滑动

javascript - 上传到服务器时出现 404 错误 codeigniter

javascript - 表单提交不起作用

关于 if 语句和位置移动的 jQuery 问题

java - 从速度模板中的字符串中删除双引号

javascript - 如何显示 foreach 循环中的前 3 个值,然后单击“更多”显示该行中的所有值