You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

原正常运行的日夜切换GIF与样式表脚本突然失效的求助

原正常运行的日夜切换GIF与样式表脚本突然失效的求助

SOLVED: 我之前不该用document.write,不过奇怪的是它一开始居然能正常工作

我做了一个脚本,用来给我的静态网站导入日夜版本的动画,现在用Github Pages托管。但我死活搞不懂,为啥导入GIF的功能突然就失效了,而且连日夜切换样式表的脚本也出了同样的问题。

出问题的是getimg函数,另一个处理日夜样式表的脚本也有问题。昨晚我最后一次提交到Github时,它还运行得毫无问题,之后我在本地只做了这些改动:

  • 给另一个页面调整了CSS(每个页面都有独立的样式表,再加上一个核心内容样式表)
  • 在另一个页面新增了一个GIF

我真的快被逼疯了,要是能得到帮助就太感谢了!

对了,这个页面原本用了W3School的HTML包含脚本,不过为了方便排查,我已经把页面顶部的这个脚本移除了,但问题还是没解决。

下面是完整的代码:

<script>
    function includeHTML() {
      var z, i, elmnt, file, xhttp;
      /* Loop through a collection of all HTML elements: */
      z = document.getElementsByTagName("*");
      for (i = 0; i < z.length; i++) {
        elmnt = z[i];
        /*search for elements with a certain atrribute:*/
        file = elmnt.getAttribute("w3-include-html");
        if (file) {
          /* Make an HTTP request using the attribute value as the file name: */
          xhttp = new XMLHttpRequest();
          xhttp.onreadystatechange = function() {
            if (this.readyState == 4) {
              if (this.status == 200) {elmnt.innerHTML = this.responseText;}
              if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
              /* Remove the attribute, and call this function once more: */
              elmnt.removeAttribute("w3-include-html");
              includeHTML();
            }
          }
          xhttp.open("GET", file, true);
          xhttp.send();
          /* Exit the function: */
          return;
        }
      }
    }
    </script>


<html>
    <head>


      <script>
        function getimg() {
          var currentTime = new Date().getHours();
          if (0 <= currentTime&&currentTime < 8) {
            document.write("<img src='assets/scottownnight.gif' id='headergif' >");
          }
          if (8 <= currentTime&&currentTime < 17) {
            document.write("<img src='assets/scottownday.gif' id='headergif' >");
          }
          if (17 <= currentTime&&currentTime < 0) {
            document.write("<img src='assets/scottownnight.gif' id='headergif' >");
          }
          
        }
        
        
        
      </script>


      <script>
        function getstyle() {
          var currentTime = new Date().getHours();
          if (0 <= currentTime&&currentTime < 8) {
            document.write("<link href='nightstyle.css' rel='stylesheet'>");
          }
          if (8 <= currentTime&&currentTime < 17) {
            document.write("<link href='style.css' rel='stylesheet'>");
          }
          if (17 <= currentTime&&currentTime < 0) {
            document.write("<link href='nightstyle.css' rel='stylesheet'>");
          }
          
        }
        
        
        
      </script>
      <link href='style.css' rel='stylesheet'>
      <link href='core.css' rel='stylesheet'>
      <script>getstyle();</script>
        
    



      <title>arron ware's scrapbook</title>
      <link rel="icon" href="jessop.ico">


      
      <meta name="description" content="projects and recipes">
      <meta name="keywords" content="arron, ware, photography, sound design, manchester, lincolnshire, cleethorpes, video, art">
      <meta name="robots" content="index, follow">
      <meta http-equiv="Content-Type" content="text/html" charset="utf-8">
      <meta charset="UTF-8">
      <meta name="language" content="English">
      <meta name="author" content="Arron Ware">
      <meta name="viewport" content="width=device-width, initial-scale=1" />

    </head>

    <body>
      <div id="logolinks">
        <h3>
            <a href=index.html style="text-decoration: inherit; color: inherit; font-family: inherit; font-size: inherit;">arron</a> ware's scrapbook
        </h3>
        <div w3-include-html="include/links.html"></div>
      </div>
        
        <script>getimg();</script>
        
        <div class="content">
            Hi!
            This is my scrapbook website.
            I keep all my my little projects and bits that I would like to have online somewhere here. <br><br>
            The website has no sole objective other than to be one large mashup of odd and ends, I hope you enjoy exploring!
            

          

        </div>

        
    </body>




</html>

<script>
    includeHTML();
</script>

备注:内容来源于stack exchange,提问作者Urban Spaceman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 03:20:36