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

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

If 語句不適用于空格鍵計數

If 語句不適用于空格鍵計數

慕勒3428872 2022-12-22 12:48:11
所以我建立了一個計數器,但我沒有設法實現 if 語句。前任:if (hits == 1) {alert("hello world1")}if (hits == 2) {alert("hello world2")}if (hits == 3) {alert("hello world3")}if (hits == 4) {alert("hello world4")}這是我的代碼    <html>    <body>    <p id="hits" value="0"> </p>    <script>      var hits = 0;      var hitElement = document.getElementById("hits");      document.body.onkeyup = function(e) {        if (e.keyCode == 32) {addHit()}      }      var addHit = function() {hits++; renderHits()}      var renderHits = function() {hitElement.innerHTML = hits % 5}      var resetHits = function() {renderHits()}    </script>    </body>    </html>另外我想知道是否有可能讓它基于 .value 而不是 .innerHTML 工作(因為我的目標是空間計數器在“幕后”工作,所以它不應該是可見的)。
查看完整描述

3 回答

?
慕容3067478

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

實際上我的目標是按輸入欄在圖像之間切換


let hits = 0;

const hitElement = document.getElementById("hits");


const images = [

    "https://via.placeholder.com/50x50.png?text=qwe", // your 1 image

    "https://via.placeholder.com/50x50.png?text=rty", // your 2 image

    "https://via.placeholder.com/50x50.png?text=uyi", // your 3 image

    "https://via.placeholder.com/50x50.png?text=opd", // your 4 image

    "https://via.placeholder.com/50x50.png?text=asd", // your 5 image

];


document.body.onkeyup = function (e) {

    if (e.keyCode === 32) {

        hits++;

        hitElement.src = images[hits % images.length];

    }

};

<p>Press spacebar</p>

<img id="hits" src="https://via.placeholder.com/50x50.png?text=qwe" />


查看完整回答
反對 回復 2022-12-22
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

    <body>

    <h1>HELLO WORLD</h1>

    <p id="hits" value="0"> 0</p>


 <script>

 var hits = 0;

 var hitElement = document.getElementById("hits");

 document.body.onkeyup = function(e) {

 if (e.keyCode == 32) {addHit()}}

 var addHit = function() {hits++; renderHits()}

 var renderHits = function() {hitElement.innerText = hits % 5}

  var resetHits = function() {renderHits()}


查看完整回答
反對 回復 2022-12-22
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

它是“點擊率 % 5”的東西。解釋器將其視為 ((lresult = a) % b) 而不是 (lresult=(a % b))。


需要括號。


let hits = 0;

const hitElement = document.getElementById("hits");


document.body.onkeyup = function (e) {

    if (e.keyCode === 32) {

        hits++;

        hitElement.src = `https://via.placeholder.com/50x50.png?text=${(hits % 5)  + 1}`;

    }

};

<p>Press spacebar</p>

<img id="hits" src="https://via.placeholder.com/50x50.png?text=1"></ing>


查看完整回答
反對 回復 2022-12-22
  • 3 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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