我可以在同一页面的同一按钮上添加顺序单击事件吗?

时间:2018-07-10 08:10:14

标签: javascript

我希望按钮可以执行多个单击任务,但是要按顺序执行,但是当我简单地添加多个“ addEventListener”时,它仅执行最后一个任务。

var btn = document.querySelectorAll(".decision")[0];
var play = document.querySelector("div");
var level = 0;

btn.addEventListener("click",function(){
    if (level===0){
        btn.innerHTML = "Hello";
        play.innerHTML = "Click again"; //not working
        level++;
    }
    if (level===1){
        play.innerHTML = "Clicked again"; //only this is working.
        level++;    
    }
});

那么有什么办法吗?

1 个答案:

答案 0 :(得分:0)

似乎一切正常,但是第二个if在第一个innerHTML之后立即执行,并且btn.addEventListener("click",function(){ if(level===0){ btn.innerHTML = "Hello"; play.innerHTML = "Click again"; //not working level++; } else if(level===1){ play.innerHTML = "Clicked again"; //only this is working. level++; } }) 的变化并不明显。请尝试:

import numpy
import matplotlib
G=numpy.zeros(100)
for i in range(50):
    G[i+50]=50-i
    matplotlib.pyplot.plot(G)