2016-07-06 28 views
0

我想启用第二个按钮,只有在第一个按钮被点击后才能被点击。我想要启用的按钮是JS中的启动变量。这是我做的:如何在发生特定操作后添加onclick事件处理程序?

<!--One of these "buttons" has to be clicked to enable clicking of another one--> 
<li id="easy" class="list" onclick="shadows(1)">Easy</li> 
<li id="medium" class="list" onclick="shadows(2)">Medium</li> 
<li id="hard" class="list" onclick="shadows(3)">Hard</li> 
<!--it goes to another function shadows()--> 

下面是函数:

function shadows(number){ 
start.style.cursor="pointer"; 
shadow=1;//I set a variable to 1(It was previously 0) 

start.style.backgroundColor="#ffffb3"; 
start.onmouseenter=function(){ 
    start.style.backgroundColor="yellow"; 
}; 
    start.onmouseleave=function(){ 
    start.style.backgroundColor="#ffffb3"; 
}; 

} 

我后来检查,如果影子变量是1

if(shadow==1){ 
start.onclick=function(){ 
easy.style.display="none"; 
medium.style.display="none"; 
hard.style.display="none"; 
randomExercise(); 
}; 
} 

我尝试添加onclick事件,但它不起作用。点击按钮时没有任何反应。我怎样才能解决这个问题?

+0

所以你希望媒体只有在轻松点击等后才可以访问? –

+0

@Erik好像我已经得到了答案。我想在点击列表中的任何项目后出现“开始”按钮。 –

回答

1

在你的阴影功能,您可以添加事件侦听器,像这样:

function shadows(number){ 
start.style.cursor="pointer"; 

start.style.backgroundColor="#ffffb3"; 
start.onmouseenter=function(){ 
    start.style.backgroundColor="yellow"; 
}; 
    start.onmouseleave=function(){ 
    start.style.backgroundColor="#ffffb3"; 
}; 

start.addEventListener("click", function(){ 
    easy.style.display="none"; 
    medium.style.display="none"; 
    hard.style.display="none"; 
    randomExercise(); 
}); 
} 

它没有工作,你怎么过它之前是因为当您加载网页的原始脚本只运行一次,究其原因,此时影子为0.这是事件处理程序如此便利的原因之一。

相关问题