重写 jQuery.find() 函数以提供额外的功能

发布于 2024-10-22 06:45:35 字数 840 浏览 1 评论 0原文

我想重写 jQuery.find 来重写额外的功能。我已经使用 Ben Nadel 博客上讨论的想法来做到这一点( http://www.bennadel.com/blog/1624-Ask-Ben-Overriding-Core-jQuery-Methods.htm )但由于某种原因它不适用于 $.find() ,这是我的代码,

        (function () {
        // Store a reference to the original remove method.
        var originalFindFunction = jQuery.find;

        // Define overriding method.
        jQuery.find = function () {
            // Log that we are calling the overridden function.
            Console.log("------> Find method called");

            // then execute the original method
            var results = originalFindFunction.apply(this, arguments);
            return results;
        };
    })();

你知道出了什么问题吗,或者我如何覆盖 jquery 函数?

I want to override jQuery.find to override extra functionality. I have used the idea discussed on Ben Nadel's blog to do so ( http://www.bennadel.com/blog/1624-Ask-Ben-Overriding-Core-jQuery-Methods.htm ) but for some reason it doesn't work for $.find(), this is my code

        (function () {
        // Store a reference to the original remove method.
        var originalFindFunction = jQuery.find;

        // Define overriding method.
        jQuery.find = function () {
            // Log that we are calling the overridden function.
            Console.log("------> Find method called");

            // then execute the original method
            var results = originalFindFunction.apply(this, arguments);
            return results;
        };
    })();

do you have an idea what's wrong, or how can I override a jquery function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-10-29 06:45:35

您参考中的演示使用了 .fn. 中的函数。

jQuery.fn.remove

的函数

jQuery.find

您要重写此级别上的函数 对于 this 具有不同的含义。
当您处理 .fn. 时 - 这是返回的查询结果,它“混合”到任何查询结果中。

这里,this将是根对象本身,并且它的用法有所不同。这些是“静态”调用的函数或实用函数。

The demonstration in your reference uses a function from .fn.

jQuery.fn.remove

Where you're overriding a function on

jQuery.find

Overriding functions on this level have different meaning for this.
When you're working on .fn. - the this is the returned query result, which are "mixed in" to any query result.

Here, the this will be the root object itself, and the usage by it is different. These are functions or utility functions that are invoked "statically".

無處可尋 2024-10-29 06:45:35

您正在寻找覆盖/挂钩到 jQuery.fn.find 而不是 jQuery.find


(function () {
    // Store a reference to the original remove method.
    var originalFindFunction = jQuery.fn.find;

    // Define overriding method.
    jQuery.fn.find = function () {
        // Log that we are calling the overridden function.
        Console.log("------> Find method called");

        // then execute the original method
        var results = originalFindFunction.apply(this, arguments);
        return results;
    };
})();

You're looking for overriding/hooking into jQuery.fn.find not jQuery.find.


(function () {
    // Store a reference to the original remove method.
    var originalFindFunction = jQuery.fn.find;

    // Define overriding method.
    jQuery.fn.find = function () {
        // Log that we are calling the overridden function.
        Console.log("------> Find method called");

        // then execute the original method
        var results = originalFindFunction.apply(this, arguments);
        return results;
    };
})();
不甘平庸 2024-10-29 06:45:35

实际上我没有看到你的代码有任何错误。该示例确实使用了 jQuery 原型(“fn”),但是您也可以操作 $.find。我基本上复制了您在以下示例中提供的相同代码:

var oldFind = $.find;

$.find = function() {
    console.log("something new.");

    console.log(oldFind.apply(this, arguments));
};

$.find("div");

意识到,您可能会以特定方式执行此操作。上面你有:

(function() {...})();

这使得第一个括号内的代码自动执行(如果我错了,请纠正我),如果您在此函数之外调用包含的代码,则可能没有所需的意图。上面代码片段的工作示例如下: http://jsfiddle.net/nuPLV/

I actually don't see any fault in your code. It's true the example uses the jQuery prototype("fn"), however you can manipulate $.find as well. I've basically copied the same code you provided in the following example:

var oldFind = $.find;

$.find = function() {
    console.log("something new.");

    console.log(oldFind.apply(this, arguments));
};

$.find("div");

Realize, you may be executing this in a specific way. Above you have:

(function() {...})();

This makes the code within the first parens, self-executing(correct me if I'm wrong here), which may not have the intent desired if you call the contained code outside this function. A working example of the snippet above is here: http://jsfiddle.net/nuPLV/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文