2016-07-06 27 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.這是事件處理程序如此便利的原因之一。

相關問題