【发布时间】:2014-05-27 09:27:15
【问题描述】:
我遇到了一个循环问题,我希望数组在小提琴中提供 iframe 的 src。我希望循环在同一个按钮按下时遍历数组,直到它到达循环结束。
这是我的代码:
HTML:
<div id="jgbutton" class="demo">
<button>Push me, Lara</button>
<p>(and again, till you like what you see)</p>
</div>
<div class="datecontentwrapper">
<iframe src="" id="iframeFILL"></iframe>
</div>
jQuery:
jQuery("#jgbutton").click(function () {
var dateplaces = ["http://www.tate.org.uk/whats-on/tate-modern/exhibition/henri-matisse-cut-outs", "http://www.bootstrapcompany.co.uk/22_dalston_roof_park", "http://commons.wikimedia.org/wiki/File:Light_Painting_1_-_Booyeembara_Park.jpg", "http://www.happinessforgets.com/", "http://www.gordonswinebar.com/defaultm.php"];
var arrayLength = dateplaces.length;
var genrandomdate = dateplaces[Math.floor(Math.random() * dateplaces.length)];
for (var i = 0; i < arrayLength; i++) {
$("#iframeFILL").attr("src", dateplaces[i]);
$(".datecontentwrapper").show('slow');
$("p").show();
}
});
JSfiddle: http://jsfiddle.net/hslincoln/8Gnuy/9/
我是在全局声明变量,而不是在 for 循环中声明变量吗?
任何帮助将不胜感激!
【问题讨论】:
-
对不起,我没听懂问题
-
代码正常,请详细说明问题。
-
你的意思是:jsfiddle.net/8Gnuy/13 ???
-
完美的@A.Wolff - 就是这样!
-
稍作调整以确认完成:jsfiddle.net/hslincoln/8Gnuy/14