jQuery:如何检测元素是否未单击? [英] jQuery: How to detect if an element is not clicked?

查看:113
本文介绍了jQuery:如何检测元素是否未单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以检测是否未单击某个元素.这是我的代码:

I'd like to know if it is possible to detect if an element is not clicked. This is the code I have:

$("#mpElement").myFeature({
            .........
            .........
            .........
    afterDo: function() {
        // This if-else statement has to go inside "when not clicked" condition. 
        // Not sure how to do this...
        if($(".myButton").last().hasClass("selected")) {
            $(".myButton").first().addClass("selected");
            $(".myButton").last().removeClass("selected");
        } else {
            $(".selected").removeClass("selected").next().addClass("selected");
        }
        $(".myButton").on("click", function(){
            $(".myButton").removeClass("selected");
            $(this).closest(".myButton").addClass("selected"); 
        });
    }
});

您会注意到 $(.myButton").on("click",function(){)用于检测点击事件.我想将if-else语句放在此函数上方在未单击时条件下.不确定如何执行此操作.

You will notice $(".myButton").on("click", function(){ for detecting click event. I'd like to put the if-else statements above this function inside when not clicked condition. Not sure how to do this.

推荐答案

您可以为该元素维护一个data属性作为单击检测器.

You can maintain a data property to that element as a click detector.

示例:

<button class="myButton" data-clicked="no">My Button</button>

在这里,我将data-clicked=no设置为初始配置.

Here, I set data-clicked=no as initial config.

click事件中,将data-clicked设置为yes,例如:

Within the click event set data-clicked to yes like:

$('.myButton').on('click', function() {
  $(this).data('clicked', 'yes');
});

现在检查该data-clicked属性,如下所示:

Now check this data-clicked property like below:

var isClicked = $('.myButton').data('clicked');

if( isClicked == 'yes') {
   // do something
} else {
  // do something else
}

这篇关于jQuery:如何检测元素是否未单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆