且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改iframe src?

更新时间:2022-04-18 06:47:27

这有效 - 使用PLAIN javascript,是的,内联处理程序 - 它应该附加在onload中,但我们一次只能做一件事

This works - using PLAIN javascript and yes, an inline handler - it should be attached in onload, but let's take it one thing at a time

DEMO HERE

<html>
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
    <script>
      var cnt=0,webpageArray = [
        "http://cnn.com/",
        "http://msn.com/", 
        "http://yahoo.com/"
      ]; 

      function loadNextPage(dir) {   
        cnt+=dir;
        if (cnt<0) cnt=webpageArray.length-1; // wrap
        else if (cnt>= webpageArray.length) cnt=0; // wrap
        var iframe = document.getElementById("myframe"); 
        iframe.src = webpageArray[cnt]; 
        return false; // mandatory!
      } 
    </script>
  </head> 

  <body>  
  <iframe id="myframe" src="http://cnn.com/"></iframe> 

  <a href="#" onclick="return loadNextPage(-1)"> << Previus Web Page </a> | 
  <a href="#" onclick="return loadNextPage(1)"> Next Web Page >> </a> 
  </body> 
  </html>






jQuery version


jQuery version

DEMO HERE

var cnt=0,webpageArray = [
  "http://cnn.com/",
  "http://msn.com/", 
  "http://yahoo.com/"
]; 

$(document).ready(function() {
  $("#prev, #next").click(function(e) {
    cnt+=this.id=="next"?1:-1;
    if (cnt<0) cnt=webpageArray.length-1; // wrap
    else if (cnt>= webpageArray.length) cnt=0; // wrap
    $("#myframe").attr("src", webpageArray[cnt]);
    return false;
  });
});    

<iframe id="myframe" src="http://cnn.com"></iframe> <br />
<a href="#" id="prev"> << Previous Web Page </a> | 
<a href="#" id="next"> Next Web Page >> </a>