亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

HTML中的背景不會根據可變數字而改變

HTML中的背景不會根據可變數字而改變

料青山看我應如是 2022-06-09 19:32:52
我正在嘗試制作一個簡單的游戲,其中背景每 20 次跳躍就會改變一次。有這個問題。當跳躍計數器達到 20 時,背景不會改變。我不知道我做錯了什么。我嘗試使用“document.getElementById('sample').innerhtml”將 CSS 代碼更改為更改背景的更新 CSS。它似乎不起作用。我檢查了 JavaScript 控制臺,它說的是“未捕獲的 TypeError:無法在 index.html:15 處設置屬性 'innerHTML' of null”。目前使用一個按鈕來測試跳躍。這是整個文件:(我知道它很亂)<html><head>    <title>Background Slide Test</title>    <script type="text/javascript">        var jump_count = 0;        //Mechanics for the character to jump (Jump counter and character movement)        function incline(){            jump_count++;            document.getElementById('counter').innerHTML = "<h1 class='counter'>Jump Count: " + jump_count + "</h1>";            document.getElementById('jump_animation').innerHTML = ".jump{animation:jump 0.4s linear;}@keyframes jump{0%{transform:translate3d(0, 0, 0);}50%{transform:translate3d(0, -300px, 0);}100%{transform:translate3d(0, 0, 0);}}";        }        //If, else if, else: Changes the scene according to amount of jumps        if (jump_count < 20){            //This willchange the background to be 'field.png'            document.getElementById('background').innerHTML = "html{background-color:black;animation:day_cycle 180s linear infinite;}.background{background:url('test.png') repeat-x;height:100%;width:3050px;animation:slide 3s linear infinite;}@keyframes slide{0%{transform:translate3d(0, 0, 0);}100%{transform:translate3d(-1600px, 0, 0);}}@keyframes day_cycle{0% {background-color:#0063fd;}15% {background-color:orange;}30% {background-color:red;}45% {background-color:#4B0082;}60% {background-color:red;}75% {background-color:orange;}100% {background-color:#0062fd;}}";        } else if (jump_count > 20 && jump_count < 40){        } else {            //Nothing happens        }        //This is where when the space button is pressed, the sprite will jump //This does nothing yet        function space(){            if (e.key === 37){                jump_count++;            } else {                //Nothing will happen            }        }    </script>
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

Uncaught TypeError: Cannot set property 'innerHTML' of null at index.html:15"。這意味著它沒有找到您要獲取的元素。console.log( document.getElementById('sample'));用于檢查輸出...如果確實找不到該元素,然后仔細檢查你的 html id。我看到的一個可能的錯誤是你有 2 個相同的 id?!氨尘啊辈粦摪l生



查看完整回答
反對 回復 2022-06-09
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

這是因為這部分:


    if (jump_count < 20){

        //This willchange the background to be 'field.png'

        document.getElementById('background').innerHTML = "html{background-color:black;animation:day_cycle 180s linear infinite;}.background{background:url('test.png') repeat-x;height:100%;width:3050px;animation:slide 3s linear infinite;}@keyframes slide{0%{transform:translate3d(0, 0, 0);}100%{transform:translate3d(-1600px, 0, 0);}}@keyframes day_cycle{0% {background-color:#0063fd;}15% {background-color:orange;}30% {background-color:red;}45% {background-color:#4B0082;}60% {background-color:red;}75% {background-color:orange;}100% {background-color:#0062fd;}}";

    } else if (jump_count > 20 && jump_count < 40){

        document.getElementById('background').innerHTML = "html{background-color:black;animation:day_cycle 180s linear infinite;}.background{background:url('field.png') repeat-x;height:100%;width:3050px;animation:slide 3s linear infinite;}@keyframes slide{0%{transform:translate3d(0, 0, 0);}100%{transform:translate3d(-1600px, 0, 0);}}@keyframes day_cycle{0% {background-color:#0063fd;}15% {background-color:orange;}30% {background-color:red;}45% {background-color:#4B0082;}60% {background-color:red;}75% {background-color:orange;}100% {background-color:#0062fd;}}";

    } else {

        //Nothing happens

    }

腳本的那部分沒有包含在函數中,因此它會立即執行。由于腳本標簽位于頭部內部,因此它無需等待頁面準備好即可執行。所以該background元素還不存在。


您可以將該部分放在頁面ready事件的處理程序中,但最簡單的做法是將整個script標簽移動到正文的末尾。


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號