Show a list of links in an iframe one at a time wi

2019-08-28 10:36发布

问题:

Ok, so I need help with a script that takes a list of urls in an array, then shows each url one at a time in an iframe, and allows me to put a border around the link to the next item of the list INSIDE the iframe. I need it to show the page for about 3 seconds, then display the next page, and the next and so on, but only one page at a time.

Here's where I'm at so far:

    var links = ["link1", "link2", "link3"];

    for (var i = 0; i < links.length; i++)
    {
        $("#viewer iframe")
            .attr('src', "http://www.mysite.com/" + links[i])
            .load(function() {
                $(this).contents().find("a[href*='" + links[i+1] + "']").css("border", "1px solid black");});
        setTimeout('$("#viewer iframe").attr("src", "")', 3000);
    }

This works to show the first link in the array and then after 3 seconds sets the iframe src to nothing, but it doesn't show the subsequent links.

回答1:

Try this:

var links = ["link1", "link2", "link3"];
var current = 0;

function showNextLink()
{
   if (current >= links.length) {
       return;
   }

   $("#viewer iframe")
        .attr('src', "http://www.mysite.com/" + links[current])
        .load(function() {
            if (current < links.length) {
                $(this).contents().find("a[href*='" + links[current+1] + "']").css("border", "1px solid black");}});

   current++;
   setTimeout(arguments.callee, 3000);
}

showNextLink();