document.head.appendChild 或 document.createElement 在 IE 中不起作用

2024-05-06

我有一个脚本在我的 html 文档的头部运行,它可以在除 Internet Explorer 之外的每个浏览器中运行。在 Opera、Safari、Chrome、Firefox、Internet Explorer 中进行了测试。

我的代码如下:

<html>
  <head>
    <script type = "text/javascript">
      var date = new Date();
      var month = date.getMonth() + 1;
      if (month >= 3 && month <= 5)
      {
        var NewScript = document.createElement("script");
        NewScript.type = "text/javascript";
        NewScript.src = "source1.js";
        var NewStyles = document.createElement("link");
        NewStyles.rel = "stylesheet";
        NewStyles.type = "text/css";
        NewStyles.href = "css1.css";
        document.head.appendChild(NewScript);
        document.head.appendChild(NewStyles);
      }
      else
      {
        var NewScript = document.createElement("script");
        NewScript.type = "text/javascript";
        NewScript.src = "source2.js";
        var NewStyles = document.createElement("link");
        NewStyles.rel = "stylesheet";
        NewStyles.type = "text/css";
        NewStyles.href = "css2.css";
        document.head.appendChild(NewScript);
        document.head.appendChild(NewStyles);
      }
    </script>
  </head>
  <body>
  <!-- MY CONTENT GOES HERE -->
  </body>
</html>

我不确定是 document.createElement 还是 document.head.appendChild 在 IE 中不起作用。如前所述,它适用于我测试过的所有其他浏览器。如果我能提供帮助,我将不胜感激,因为我将继续自己寻找问题/解决方案。谢谢!


Try document.getElementsByTagName('head')[0]代替document.head

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

document.head.appendChild 或 document.createElement 在 IE 中不起作用 的相关文章

随机推荐