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

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

圖片正在加載為空

圖片正在加載為空

UYOU 2021-05-08 17:13:45
我是Java語言的新手,但我嘗試使用onload()設置默認圖像,但我認為它無法達到我在數組中設置的圖像,因此我不知道為什么。單擊按鈕后,我將使它旋轉圖像,但是我什至無法獲得要添加的默認圖像。var images = ['img/profile.jpg', 'img/mountain.jpg', 'img/sanfran.jpg'];var num=1;function loadPage(){    document.getElementById("pictures").src = images[0].src;}function nextImage(pictures){    var img = document.getElementById("pictures");    document.getElementById("pictures").src = images[1].src;    console.log(num++); // I have this just to make sure that it is catching something}<div id="maincontent">  <div id="pictures">     <img src="img/mountain.jpg">  </div>  <div id="paragraph">    <p>    </p>    <button onclick="nextImage()">Switch Images</button>  </div></div>
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

這里的圖像是一個javascript數組。因此,您不能使用.src,因為它不是HTML img元素


var images = ['img/profile.jpg', 'img/mountain.jpg', 'img/sanfran.jpg'];

var num=1;


//This function will run on body onload

function loadPage()

{

    document.getElementById("pictures").src = images[0];

}


//This function will run on button click

function nextImage()

{

    var img = document.getElementById("pictures");

    document.getElementById("pictures").src = images[1];

    console.log(num++); // I have this just to make sure that it is catching something

}

<body onload="loadPage()">

    <div id="maincontent">

        <img id="pictures" src="img/mountain.jpg">

        <div id="paragraph">

            <p>

            </p>

            <button onclick="nextImage()">Switch Images</button>

        </div>

    </div>

</body>


查看完整回答
反對 回復 2021-05-20
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

試試這個:


<script>

var images = ['img/profile.jpg', 'img/mountain.jpg', 'img/sanfran.jpg'];

var num=0;


function loadPage()

{

    document.getElementById("pictures").src = images[0].src;

}


function nextImage(pictures)

{

    if(num<images.length-1) {

        num = num+1;

    } else {

    num = 0;

    }


    var img = document.getElementById("pictures");

    document.getElementById("pictures").src = images[num];

    console.log(num); // I have this just to make sure that it is catching something

}

</script>

<div onload="loadPage()">

  <div id="imageholder">

     <img id="pictures" src="img/mountain.jpg">

  </div>

  <div id="paragraph">

    <p>

    </p>

    <button onclick="nextImage()">Switch Images</button>

  </div>

</div>


查看完整回答
反對 回復 2021-05-20
  • 3 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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