How to bind both Mousedown and Touchstart, but not respond to both? Android, JQuery

后端 未结 9 886
无人及你
无人及你 2020-12-04 14:24

Working on a website that is also viewable on mobile and need to bind an action on both touchstart and mousedown.

Looks like this

 $(\"#roll\").bind         


        
9条回答
  •  盖世英雄少女心
    2020-12-04 14:41

    I have been using this function:

    //touch click helper
    (function ($) {
        $.fn.tclick = function (onclick) {
    
            this.bind("touchstart", function (e) { 
                onclick.call(this, e); 
                e.stopPropagation(); 
                e.preventDefault(); 
            });
    
            this.bind("click", function (e) { 
               onclick.call(this, e);  //substitute mousedown event for exact same result as touchstart         
            });   
    
            return this;
        };
    })(jQuery);
    

    UPDATE: Modified answer to support mouse and touch events together.

提交回复
热议问题