Click and double-click event firing

后端 未结 3 1331
长情又很酷
长情又很酷 2021-01-22 01:35

I\'m writing event handlers using jQuery, and had a question about the click and double-click events. I\'ve got an element with a class \"file\" on it. It has two event handlers

相关标签:
3条回答
  • 2021-01-22 02:11

    Dont trust the first answers, thats really really bad. Use this instead:

    $('.file').on('click', function()
    {
        $(this).css('background', 'blue');
    });
    
    $('.file').on('dblclick', function(event){
        event.preventDefault();
        $(this).css('background', 'green');                               
    });
    

    http://jsfiddle.net/H42KV/

    0 讨论(0)
  • 2021-01-22 02:19

    This is the default behavior of jquery. You must use/do something like this: https://gist.github.com/399624

    0 讨论(0)
  • 2021-01-22 02:27

    As James mentions in his comment, there cannot be one without the other. It's going to be a bit of a hack, but you can have these work independently with a timer:

    $('.file').live('dblclick', function() {
    
    
      $(this).data('doubleClicked', true);
      //event handler code
    
    });
    
    function SingleClickHandle(this)
    {
      if (! $(this).data('doubleClicked'))
      {
      //event handler code
      }
    }
    
    $('.file').live('click', function(){
    
      $(this).data('doubleClicked', false);
      setTimeout('SingleClickHandler(this)', 500); // is half a second enough to consider it a double-click?
    
    });
    
    0 讨论(0)
提交回复
热议问题