2015-09-10 100 views
-1

我想點擊一個div,並有一個JavaScript函數重置 每次我點擊id =「home」按鈕我想countME()和countDE()復位至1/5如何觸發一個功能,點擊一個div id javascript/lQuery

HTML

<button type="button" onClick="countME()">Click up</button> 
<button type="button" onClick="countDE()">Click down</button> 
<div id="home"><button>home</button></div> 
<p>Clicks: <a id="clicks">1</a>/5</p> 

JS

var clicks = 1; 
function countME() { 
    clicks += 1; 
    if(clicks >= 5){ 
      clicks = 5; 
     } 
    document.getElementById("clicks").innerHTML = clicks; 
} 
function countDE() { 
    clicks -= 1; 
    if(clicks <= 1){ 
      clicks = 1; 
     } 
    document.getElementById("clicks").innerHTML = clicks; 
} 

jfiddle

+1

帶有id ='home''的元素是div – DDan

回答

1

其實都一樣,你做之前:

<button type="button" onClick="countME()">Click up</button> 
<button type="button" onClick="countDE()">Click down</button> 
<div id="home"><button onClick="resetDE()" >home</button></div> 
    <p>Clicks: <a id="clicks">1</a>/5</p> 
var clicks = 1; 
    function countME() { 
     clicks += 1; 
     if(clicks >= 5){ 
       clicks = 5; 
      } 
     document.getElementById("clicks").innerHTML = clicks; 
} 
    function countDE() { 
     clicks -= 1; 
     if(clicks <= 1){ 
       clicks = 1; 
      } 
     document.getElementById("clicks").innerHTML = clicks; 
} 

function resetDE() { 
     clicks = 1; 
     document.getElementById("clicks").innerHTML = clicks; 
} 

在這種情況下你不需要的jQuery。