重写 jQuery.find() 函数以提供额外的功能
我想重写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您参考中的演示使用了
.fn.
中的函数。的函数
您要重写此级别上的函数 对于
this
具有不同的含义。当您处理
.fn.
时 - 这是返回的查询结果,它“混合”到任何查询结果中。这里,
this
将是根对象本身,并且它的用法有所不同。这些是“静态”调用的函数或实用函数。The demonstration in your reference uses a function from
.fn.
Where you're overriding a function on
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".您正在寻找覆盖/挂钩到
jQuery.fn.find
而不是jQuery.find
。You're looking for overriding/hooking into
jQuery.fn.find
notjQuery.find
.实际上我没有看到你的代码有任何错误。该示例确实使用了 jQuery 原型(“fn”),但是您也可以操作 $.find。我基本上复制了您在以下示例中提供的相同代码:
意识到,您可能会以特定方式执行此操作。上面你有:
这使得第一个括号内的代码自动执行(如果我错了,请纠正我),如果您在此函数之外调用包含的代码,则可能没有所需的意图。上面代码片段的工作示例如下: 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:
Realize, you may be executing this in a specific way. Above you have:
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/