js 右击事件

一、该方法无法获取$(this)

$.fn.extend({
        "rightclick": function (fn) {
            $(this).mousedown(function (e) {
                if (e.which === 3) {
                    $(this).bind("contextmenu", function (e) {
                        return false;
                    });
                    fn(e);
                }
            });
            return this;
        });

二、该方法可以获取$(this)

 $.fn.extend({
        "rightclick": function (fn) {
             return this.mousedown(function (event) {
                 if (event.which === 3) {
                     $(this).bind("contextmenu", function (e) {
                         return false;
                     });
                     return fn.apply(this, arguments) || false;
                 }
             });

});

原文地址:https://www.cnblogs.com/johnblogs/p/10239035.html