如何在点击div id javascript / lQuery时触发一个函数

时间:2015-09-10 06:40:48

标签: javascript jquery html

我正在尝试单击div并重置javascript函数 每次我点击id =" home"我希望countME()和countDE()重置为1/5

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 个答案:

答案 0 :(得分: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。