0

我是 Javascript 的新手,我不知道如何循环遍历一组 html 文档以使用单击下一步按钮一次填充一个 iFrame。这是我一直在努力的

window.onload = loadPages;

function loadPages () {
        var frameThem = document.getElementById("frameWrap");
        var arr = ["aCard.html", "bCard.html", "cCard.html", "dCard.html"];

        for (index = 0;index < arr.length; index++) {

            frameThem.src = (arr[index]);
        }

};

document.getElementById('nextButton').onclick = loadPages();

这只会加载最后一个 html 文档。我很感激我能得到的任何帮助。谢谢。

4

2 回答 2

2
var index = 0;
var arr = ["aCard.html", "bCard.html", "cCard.html", "dCard.html"];

function loadPages () {
    document.getElementById("frameWrap").src = (arr[index]);
    index = (index + 1) % arr.length;
};

window.onload = loadPages;
document.getElementById('nextButton').onclick = loadPages();
于 2013-10-17T17:26:01.627 回答
0

此代码将一一显示,单击按钮时显示下一个。

<!DOCTYPE html>
<html>
<body>
<button id="nextButton">NEXT!</button>
<iframe id="frame1"></iframe>
<script type="text/javascript">
    var lastloaded = 0;
    window.onload = loadPages;

    function loadPages () {
        var frame = document.getElementById("frame1");
        var arr = ["aCard.html", "bCard.html", "cCard.html", "dCard.html"];
        if (lastloaded+1>arr.length){
            lastloaded = 0;
        }
        frame.src = arr[lastloaded];
        lastloaded++;
        }
        document.getElementById('nextButton').onclick = loadPages;
</script>
</body>
</html>
于 2013-10-17T17:47:48.567 回答