简体   繁体   English

扩展on()函数的jQuery插件不接受“ this”

[英]jQuery plugin to extend on() function doesn't accept “this”

I'm writing a plugin to extend the on() function of jQuery to implement mousetrap.js 我正在编写一个插件来扩展jQuery的on()函数以实现mousetrap.js

I should get from the function this and pass it to on() . 我应该从函数this获取并将其传递给on()

This is my code: 这是我的代码:

    $.fn.superon = function (keys, myfn) {
        $("body").on("click", this, function (e) {
            myfn();
        });
        Mousetrap.bind(keys, function () {
            myfn();
        });
    };
    $.fn.superoff = function (keys) {
        $("body").off(this);
        Mousetrap.unbind(keys);
    };

The problem is that if I use it the event is attached to all the elements and not only the one selected. 问题是,如果使用它,事件将附加到所有元素,而不仅是所选元素。

http://jsfiddle.net/zkLMX/1/ http://jsfiddle.net/zkLMX/1/

What am I doing wrong? 我究竟做错了什么?

  1. You are missing event type "click" in .off function. 您缺少.off函数中的事件类型"click"
  2. Second argument to .on function in your case has to be selector (what you are passing is jQuery object) 在您的情况下, .on函数的第二个参数必须是选择器(您要传递的是jQuery对象)

Following is the updated code 以下是更新的代码

(function ($) {
    $.fn.superon = function (selector, keys, myfn) {
        $("body").on("click", selector, function (e) {
            myfn();
        });
        Mousetrap.bind(keys, function () {
            myfn();
        });
        return this;
    };
    $.fn.superoff = function (selector, keys) {
        $("body").off("click", selector);
        Mousetrap.unbind(keys);
        return this;
    };
})(jQuery);


$("#click").superon("#click", "a", function () {
    alert("hello");
    $("#click").superoff("a");
});

});

$("#click2").superon("#click2", "b", function () {
    alert("hello2");
    $("#click2").superoff("b");
});

Hope it helps... 希望能帮助到你...

This is my solution, thanks for the suggestion of @devnull69. 这是我的解决方案,感谢@ devnull69的建议。

 // on() with keyboard shortcut support
 $.fn.superon = function (selector, keys, myfn) {
   this.on("click", selector, function (e) {
     myfn();
   });
   Mousetrap.bind(keys, function () {
     myfn();
   });
   return this;
 };

 // off() with keyboard shortcut support
 $.fn.superoff = function (selector, keys) {
   this.off("click", selector);
   Mousetrap.unbind(keys);
   return this;
 };

In this way I will call this in the same way of how I would call the on() and off() functions. 这样,我将以与调用on()off()函数相同的方式进行调用。

The second parameter to .on() is a selector string, not an element. .on()的第二个参数是选择器字符串,而不是元素。 You should just omit the second parameter and bind the click to this instead of body 您只需要忽略第二个参数,然后将点击绑定this而不是body

$.fn.superon = function (keys, myfn) {
    this.on("click", function (e) {
        myfn();
    });
    Mousetrap.bind(keys, function () {
        myfn();
    });
};
$.fn.superoff = function (keys) {
    this.off("click");
    Mousetrap.unbind(keys);
};

http://jsfiddle.net/zkLMX/4/ http://jsfiddle.net/zkLMX/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM