Disable doubleclick event for an element in Opera

2019-02-15 08:02发布

问题:

Is there a way to disable (with CSS, JS or jQuery) double-click for a given element?

The problem with Opera is that it displays a menu when I click on an element too fast. Note that I know how to disable this for me. I'd like to be able to disable this for all user that use the script.

The buttons in question are "next"/"previous" buttons and I use input type image for them, but the same happens with "a".

回答1:

It turended out I need this:

/**
    Disable text selection by Chris Barr, of chris-barr.com
*/
$.fn.disableTextSelect = function() {
    return this.each(function(){
        if($.browser.mozilla){//Firefox
            $(this).css('MozUserSelect','none');
        }else if($.browser.msie){//IE
            $(this).bind('selectstart',function(){return false;});
        }else{//Opera, etc.
            $(this).mousedown(function(){return false;});
        }
    });
}

And then I could disable text selection on my button elements like this:

$(function(){ $('input[type=image]').disableTextSelect(); });

And now I can click buttons fast as hell and all works fine :-).



回答2:

You cannot have a click and dblclick event handler attached on the same element because when you dblclick both the events are going to be triggered. In order to make it work there are few work arounds.

This might help you

Need to cancel click/mouseup events when double-click event detected

Looking at your problem there is a simple solution. In the click event handler once it is clicked set a disabled attribute or some class name(disabled). In the handler before exectuing your code checck for this attribute or class name. If it exists then dont do anything. After sometime remove this attribtue or class name. Try this

$("selector").click(function(){
   var $this = $(this);
   if(!$this.hasClass("disabled")){


       //Do you stuff here


       $this.addClass("disabled");

       setTimeout(function(){
         $this.removeClass("disabled");
       }, 200);  

   }
});


回答3:

JavaScript would do that for you.

DOMElement.ondblclick = (function () {return false;})();