使用js / jquery遍历要在iframe中加载的html文件列表

时间:2015-10-29 14:47:04

标签: javascript jquery html iframe

我有一组HTML文件,我想通过iframe循环播放,我不太确定如何设置javascript,因为我几乎没有经验。这是我试图让这个工作的原因:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset = 'utf-8'>
    <title>Sankey Plot Test</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  </head>

  <body>
    <iframe src = "plot1.html" width = 100% height = 100% id = "sankey"></iframe>
    <script type = "text/javascript">
      (function() {
        var selector = document.getElementById('sankey'),
        var delay_sec = 1,
        var num = 0,
        var len = 2;
        setInterval(function() {
          num = (num === len) ? 0 : num;
          selector.src = "plot" + num + ".html";
          num++;
        }, delay_sec * 50);
      }());
    </script>
  </body>
</html>

上面的代码是偶然通过我在SO上看到的其他答案扔在一起的。我认为这会像(伪代码)那样简单:

<script>
  var counter = 0;
  for (i = 0, i < eternity, i++) {
    counter++;
    counter = (counter === 2) ? 0 : counter;
    var source = "plot" + counter + ".html";
    $("#sankey").load(source);
    sleep(2 seconds);
  }
</script>

我哪里错了?

与主要问题无关的副问题:为什么在我的第一个代码块中,js代码中是否有逗号而不是分号?

1 个答案:

答案 0 :(得分:1)

nonProxiedHosts=localhost

您可以在JavaScript中以某种列表格式定义变量,如下所示:

// You should also only do DOM manipulation after DOM ready and the
//   easiest way to do that is to pass a function directly to jQuery
//   like so
$(function() {
    // I'm just guessing you mean to use jQuery since you included it
    //  #sankeyis the CSS syntax for selecting an element with the
    //  ID 'sankey' and jQuery uses CSS syntax for selecting elements
    var selector = $('#sankey');
    var delay_sec = 10;
    var num = 0,
        len = 2;
    setInterval(function() {
        num = (num === len) ? 0 : num;
        //this is how you set an attribute on a jQuery selector
        selector.attr('src', "plot" + num + ".html");
        num++;
    //the second argument to setTimeout/setInterval is in milliseconds
    }, delay_sec * 1000);
});

相当于:

var i = 0,
    j = 1, 
    k = 2;

这是语法错误:

var i = 0;
var j = 1;
var z = 2;