Javascript jQuery 鼠标点击计数器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12319598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:42:59  来源:igfitidea点击:

jQuery mouse click counter

javascriptjqueryhtmlclickmouseevent

提问by Milan Vu?kovi?

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 次点击?

回答by Danil Speransky

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

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

(function () {
  var count = 0;

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

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

回答by Constantinius

You can use jQuery's toggleClassfunction for that:

您可以toggleClass为此使用 jQuery 的函数:

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

When clicked once, the element has the someClassclass, and when clicked twice, the class is removed again.

单击一次时,该元素具有someClass该类,单击两次时,该类再次被删除。

回答by Michal Klouda

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 参数的值,从匹配元素集中的每个元素中添加或删除一个或多个类。