开发者

How to see if you're clicking the same element twice? Jquery

开发者 https://www.devze.com 2023-03-20 08:49 出处:网络
开发者_如何学PythonHow can you detect if the user is clicking the same div? I\'ve tried this with no success:
开发者_如何学Python

How can you detect if the user is clicking the same div?

I've tried this with no success:

    _oldthis = null;

    var _this = $(this);

    if(_oldthis == _this) {
        alert('You clicked this last');
    }

    _oldthis = _this;


You can't compare jQuery objects, but you can compare the DOM objects they contain. Try this:

var previousTarget=null;
$("a").click(function() {
    if(this===previousTarget) {
        alert("You've clicked this element twice.");
    }
    previousTarget=this;
    return false;
});


You may try this,

var _oldthis = null;
$('div').click(function(){
  var _this = $(this);

  if(_this == _oldthis) {
    alert('You clicked this last');
    _oldthis = null;
    return false;
  }
  _oldthis = _this;

});


var clickHandler;
$('a').click(function(){
    if(clickHandler) {
        clickHandler = alert('click twice');
    } else {
        clickHandler = alert('click once');
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消