【发布时间】:2014-03-18 06:41:43
【问题描述】:
我们正在尝试制作带有按钮和计时器的报价滚轮。我们已经走了很远,但此时按钮无法正常工作,这是一个 jsfiddle:http://jsfiddle.net/BaXXg/1/
这里是代码
var quotes = [
"Quote1<br><br> - Repomeri",
"Quote2 <br><br> - Emmi",
"Quote3<br><br> - Taateli",
"Quote4<br><br> - Joonas",
"Quote5<br><br> - Eskelinen",
];
var i = 0;
var timer = null;
setInterval(function() {
$("#slideContainer").html(quotes[i]);
if (i == quotes.length) {
i = 0;
} else {
i++;
}
}, 4 * 1000);
var b1 = document.getElementById('b1'),
b2 = document.getElementById('b2'),
b3 = document.getElementById('b3'),
b4 = document.getElementById('b4'),
b5 = document.getElementById('b5');
b1.onclick = function() {
$("#slideContainer").html(quotes[i]);
i = 0;
};
b2.onclick = function() {
$("#slideContainer").html(quotes[i]);
i = 1;
};
b3.onclick = function() {
$("#slideContainer").html(quotes[i]);
i = 2;
};
b4.onclick = function() {
$("#slideContainer").html(quotes[i]);
i = 3;
};
b5.onclick = function() {
$("#slideContainer").html(quotes[i]);
i = 4;
};
【问题讨论】:
-
您可以简化代码,因为您已经在使用 jQuery。看我更新的演示jsfiddle.net/Godinall/BaXXg/8你最后30行代码实际上可以用jQuery写成3行。
标签: javascript jquery html slider quotes