在iframe中旋转网址

ton*_*nyf 7 javascript

我有10个不同的网址,我想要输入iframe src属性,我也想旋转说iframe中所有10个网址之间每隔5秒.

不确定如何使用javascript/best方法执行此操作?

对不起,应该提到我使用的是IE6.

谢谢.

gbl*_*zex 7

<iframe id="rotator" src="http://...first"></iframe>

<script>
// start when the page is loaded
window.onload = function() {

  var urls = [
    "http://...first",
    "http://...second",
    // ....
    "http://...tenth" // no ,!!
  ];

  var index = 1;
  var el = document.getElementById("rotator");

  setTimeout(function rotate() {

    if ( index === urls.length ) {
      index = 0;
    }

    el.src = urls[index];
    index  = index + 1;

    // continue rotating iframes
    setTimeout(rotate, 5000);

  }, 5000); // 5000ms = 5s
};
</script>
Run Code Online (Sandbox Code Playgroud)


Jas*_*ary 0

方法有很多,所以最好的方法还有待讨论。setInterval()既然你提到了 JavaScript,请看一下。id我将编写一个方法,通过其属性获取页面上的 iframe ,getElementById()并将该src属性更改为 URL 数组中的下一个 URL。