Disable doubleclick event for an element in Opera

安稳与你 提交于 2019-11-30 18:57:09

问题


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;})();


来源:https://stackoverflow.com/questions/7203286/disable-doubleclick-event-for-an-element-in-opera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!