gpt4 book ai didi

javascript - .js 文件中的重新加载按钮用于简单的幻灯片放映,无法使其工作

转载 作者:行者123 更新时间:2023-11-28 09:37:29 25 4
gpt4 key购买 nike

已解决

大家好,我刚刚开始学习基本的 javascript,只是想知道如何从按钮调用此函数,同时将所有代码保留在 gallery.js 中

<input type="button" value="Reload Page" onClick="window.location.reload()">  

gallery.js 文件

var slideShow = new Array();
slideShow[0]="images/1.jpg";
slideShow[1]="images/2.jpg";
slideShow[2]="images/3.jpg";
slideShow[3]="images/4.jpg";
slideShow[4]="images/5.jpg";//loads pictures
slideShow[5]="images/6.jpg";
var thisPic = 0;

function reloadURL()
{
window.location.reload();
}

function processNext() {
if (document.images && thisPic < 5) { //process checks if this pic is less than 2
thisPic++;
document.getElementById("picture").src=slideShow[thisPic];
else if( window.location.reload()
}
}
window.onload = init; //initially loads

function init() {
document.getElementById("next").onclick=processNext; //on button click next picture is shown


}

画廊.html

<div id="content">
<h2>Gallery</h2>
<p>This is the gallery</p>
<img src="images/1.jpg" id="picture" alt="displays a slide show of 5 pictures"/><br/>
<input type="button" value="Press this button for next picture" id="next" />
<input type="button" value="Reload" onclick="reloadURL();" />

</div>

最佳答案

首先,您应该将 JavaScript 文件包含在 <head> 中html 文件的部分:

<script type="text/javascript" src="gallery.js"></script>

然后您可以调用该文件中包含的任何函数:

<input type="button" value="Reload Page" onClick="processNext();">

关于javascript - .js 文件中的重新加载按钮用于简单的幻灯片放映,无法使其工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com