【问题标题】:even and odd number of clicks count in jqueryjquery中的偶数和奇数点击计数
【发布时间】:2011-11-22 08:05:17
【问题描述】:

我的 html 页面中有四个 div,如下所示: 工作小提琴实现在这里:http://jsfiddle.net/GxaCK/3/

 <div class="first">
    <p> First </p>
    </div>
    <div class="second">
    <p> Second </p>
    </div>
    <div class="third">
    <p> Third </p>
    </div>
    <div class="fourth">
    <p> Fourth </p>
    </div>

然后是这样的jquery:

$(function() {
    var firstCnt = 0,
        secondCnt = 0,
        thirdCnt = 0,
        fourthCnt = 0;
    $(".first").live("click", function(){
        firstCnt += 1;
        $(".first p").text(firstCnt);
    });

     $(".second").live("click", function(){
        secondCnt += 1;
        $(".second p").text(secondCnt);
    });

});

现在考虑到这一点,我可以执行 firstCnt%2 来获取 div 是否被单击了偶数次或奇数次。

我有以下疑问: 1.在项目中这是一种很好的实施方式吗? 2. 有没有类似奇偶点击的选择器,as exists in css3

【问题讨论】:

  • 我认为鼠标事件没有奇怪的点击跟踪器

标签: jquery


【解决方案1】:

对于偶数..您可以使用toggle。请参阅更新的 jsfiddle here

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多