繁体   English   中英

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

[英]Using js/jquery to loop through a list of html files to load in an 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代码中使用逗号而不是分号?

// 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);
});

您可以像这样在JavaScript中以某种列表格式定义变量:

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

这等效于:

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

这是语法错误:

var i = 0, var j = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM