Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 制作带有按钮和计时器的报价辊_Javascript_Jquery_Html_Slider_Quotes - Fatal编程技术网

Javascript 制作带有按钮和计时器的报价辊

Javascript 制作带有按钮和计时器的报价辊,javascript,jquery,html,slider,quotes,Javascript,Jquery,Html,Slider,Quotes,我们正在尝试制作一个带有按钮和计时器的报价辊。我们已经走了相当远的路,但在这一点上,按钮无法正常工作。下面是一个JSFIDLE: 这是密码 var quotes = [ "Quote1<br><br> - Repomeri", "Quote2 <br><br> - Emmi", "Quote3<br><br> - Taateli", "Quote4<br><br>

我们正在尝试制作一个带有按钮和计时器的报价辊。我们已经走了相当远的路,但在这一点上,按钮无法正常工作。下面是一个JSFIDLE:

这是密码

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;
};
var引号=[
“Quote1

-Repomeri”, “引用2

-Emmi”, “Quote3

-Taateli”, “Quote4

-Joonas”, “引用5

-Eskelinen”, ]; var i=0; var定时器=null; setInterval(函数(){ $(“#slideContainer”).html(引号[i]); if(i==quotes.length){ i=0; }否则{ 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=函数(){ $(“#slideContainer”).html(引号[i]); i=0; }; b2.onclick=函数(){ $(“#slideContainer”).html(引号[i]); i=1; }; b3.onclick=函数(){ $(“#slideContainer”).html(引号[i]); i=2; }; b4.onclick=函数(){ $(“#slideContainer”).html(引号[i]); i=3; }; b5.onclick=函数(){ $(“#slideContainer”).html(引号[i]); i=4; };
我想在调用
html(引号[I])
之前,您必须将作业放到
I
。例如:

b1.onclick = function() {
    i = 0;
    $("#slideContainer").html(quotes[i]);
};

必须在
$(“#slideContainer”).html(引号[i])之前设置变量
i

您还可以使用
class
并减少代码:

HTML:


您可以简化代码,因为您已经在使用jQuery。看我更新的演示,最后30行代码实际上可以在jQuery中用3行代码编写。
<input type="button" class="btn" value="" id="b1">
<input type="button" class="btn" value="" id="b2">
<input type="button" class="btn" value="" id="b3">
<input type="button" class="btn" value="" id="b4">
<input type="button" class="btn" value="" id="b5">
$('.btn').click(function () {
    i = $(this).attr('id').substr(1);
    i--;
    $("#slideContainer").html(quotes[i]);   
});