按下时,Addeventlistener单击不更改变量

时间:2014-06-18 10:43:48

标签: javascript

我在我的网站上制作幻灯片活动,我想知道如何更改变量i

javascipt

var list = document.getElementById('list1');
list.addEventListener("click", press());

var slideshow = document.getElementById('slideshow');
var bilder = ['bild1.png','bild2.png','bild3.png'];
var totalpics = mypics.length;
var i = 0;
var a= 0

function press(){
i=0;
}

function loop() {
    if(i > (totalpics - 1)){
    i = 0;  
    }
slideshow.innerHTML = '<img src="bilder/'+bilder[i]+'">';
i++;
loopTimer = setTimeout ('loop()',3000);
}
loop();

HTML

<div id="slideshow">

</div>
<li id="list1"></li>
<li id="list2"></li>
<li id="list3"></li>

当我按i时,为什么li id="list1"不会更改为0?

1 个答案:

答案 0 :(得分:1)

您需要函数本身而不是其调用的结果

list.addEventListener("click", press);

和btw

loopTimer = setTimeout (loop, 3000);