1

解决了

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

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

画廊.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>
4

2 回答 2

0

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

于 2012-10-10T12:11:25.643 回答
0

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

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

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

<input type="button" value="Reload Page" onClick="processNext();">
于 2012-10-10T12:13:16.470 回答