为了账号安全,请及时绑定邮箱和手机立即绑定

图片正在加载为空

图片正在加载为空

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 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信