简体   繁体   English

jQuery鼠标点击计数器

[英]jQuery mouse click counter

I need to color a table in zebra style, and then when I click on the table, twice (not double click), it should change back to original. 我需要用斑马风格为桌子上色,然后当我点击桌子两次(不是双击)时,它应该变回原始状态。

My question is, how to count 2 clicks? 我的问题是,如何计算2次点击?

Demo: http://jsfiddle.net/aztVY/ 演示: http//jsfiddle.net/aztVY/

(function () {
  var count = 0;

  $('table').click(function () {
    count += 1;

    if (count == 2) {
      // come code
    }
  });
})();

You can use jQuery's toggleClass function for that: 您可以使用jQuery的toggleClass函数:

$(" ... ").click(function() {
    $(this).toggleClass("someClass");
});

When clicked once, the element has the someClass class, and when clicked twice, the class is removed again. 单击一次时,该元素具有someClass类,当单击两次时,该类将再次被删除。

I might be wrong, but in between the lines of your question I read that you actually ask about toggleClass() method documented here . 我可能错了,但在你的问题之间我读到你实际上问的是这里记录的toggleClass()方法。

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument. 根据类的存在或switch参数的值,从匹配元素集中的每个元素添加或删除一个或多个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM