How do I dynamically change the content in an iframe using jquery?

后端 未结 3 1801
夕颜
夕颜 2020-11-30 21:47

I was wondering if it is possible to have a site with an iframe and some jquery code that changes the iframe content every 30 seconds. The content is in different webpages.<

相关标签:
3条回答
  • 2020-11-30 22:11
    var handle = setInterval(changeIframe, 30000);
    var sites = ["google.com", "yahoo.com"];
    var index = 0;
    
    function changeIframe() {
      $('#frame')[0].src = sites[index++];
      index = index >= sites.length ? 0 : index;
    }
    
    0 讨论(0)
  • 2020-11-30 22:12

    If you just want to change where the iframe points to and not the actual content inside the iframe, you would just need to change the src attribute.

     $("#myiframe").attr("src", "newwebpage.html");
    
    0 讨论(0)
  • 2020-11-30 22:29
    <html>
      <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script>
          $(document).ready(function(){
            var locations = ["http://webPage1.com", "http://webPage2.com"];
            var len = locations.length;
            var iframe = $('#frame');
            var i = 0;
            setInterval(function () {
                iframe.attr('src', locations[++i % len]);
            }, 30000);
          });
        </script>
      </head>
      <body>
        <iframe id="frame"></iframe>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题