[英]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.