第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何獲取數(shù)組上的第二個(gè)索引

如何獲取數(shù)組上的第二個(gè)索引

我正在嘗試迭代圖像數(shù)組,單獨(dú)顯示每個(gè)圖像并使用按鈕移至下一個(gè)圖像。但是,當(dāng)單擊按鈕時(shí),它會移動到下一個(gè)圖像,但在第二個(gè)圖像之后,它不會移動到第三個(gè)圖像,等等任何人都可以找到循環(huán)卡住的地方嗎?function changeImage() {  var imageArray = ["/Users/rate/images/images/practice/sun1.jpg", "/Users/rate/images/images/practice/sun2.jpg", "/Users/rate/images/images/practice/sun3.jpg", "/Users/rate/images/images/practice/sun4.jpg"]  var currentImage = 0;  currentImage += 1;  if (currentImage >= imageArray.length) {    currentImage = 0;  }  document.getElementById("mainImage").src = imageArray[currentImage]}<!DOCTYPE html><html><head>  <title> App Icons? </title>  <link rel="stylesheet" type="text/css" href="/Users/Website/css/mainstyle.css">  <script type="text/javascript" src="/Users/rate/js/main.js"></script></head><body class="body">  <div>    <div>      <hr>      <div class=topbar>        <h1> Do you find this App Icon aesthically pleasing</h1>      </div>      <hr>      <div id=displayedImage> </div>      <img src="/Users/rate/images/images/practice/sun1.jpg" id="mainImage" />      <div id=answer>        <label> Strongly Agree </label>        <label><input type="radio" name="answer" value="1" required><span>1</span></label>        <label><input type="radio" name="answer" value="2" required><span>2</span></label>        <label><input type="radio" name="answer" value="3" required><span>3</span></label>        <label><input type="radio" name="answer" value="4" required><span>4</span></label>        <label><input type="radio" name="answer" value="5" required><span>5</span></label>        <label> Strongly disagree</label>        <hr>      </div>      <input type="button" onclick="changeImage()" value="I am complete">
查看完整描述

1 回答

?
眼眸繁星

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊

首先,范圍。您currentImage應(yīng)該全局聲明,否則它將始終從 0 開始。其次,數(shù)組中最后一個(gè)元素的索引等于數(shù)組長度減 1,因此您需要currentImage檢查imageArray.length - 1:


var currentImage = 0;


function changeImage() {


  var imageArray = ["/Users/rate/images/images/practice/sun1.jpg", "/Users/rate/images/images/practice/sun2.jpg", "/Users/rate/images/images/practice/sun3.jpg", "/Users/rate/images/images/practice/sun4.jpg"]


  if (currentImage >= imageArray.length - 1) {

    currentImage = 0;

  } else {

    currentImage += 1;

  }

  document.getElementById("mainImage").src = imageArray[currentImage]

  console.log(currentImage);


}

<div>

  <div>

    <hr>

    <div class=topbar>

      <h1> Do you find this App Icon aesthically pleasing</h1>

    </div>

    <hr>


    <div id=displayedImage> </div>

    <img src="/Users/rate/images/images/practice/sun1.jpg" id="mainImage" />


    <div id=answer>

      <label> Strongly Agree </label>

      <label><input type="radio" name="answer" value="1" required><span>1</span></label>

      <label><input type="radio" name="answer" value="2" required><span>2</span></label>

      <label><input type="radio" name="answer" value="3" required><span>3</span></label>

      <label><input type="radio" name="answer" value="4" required><span>4</span></label>

      <label><input type="radio" name="answer" value="5" required><span>5</span></label>

      <label> Strongly disagree</label>

      <hr>



    </div>


    <input type="button" onclick="changeImage()" value="I am complete">


查看完整回答
反對 回復(fù) 2023-07-20
  • 1 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號