本文介绍了扩展现有的 jQuery 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个插件来扩展 jQuery 中的现有功能,例如
I am trying to write a plugin that will extend an existing function in jQuery, e.g.
(function($)
{
$.fn.css = function()
{
// stuff I will be extending
// that doesn't affect/change
// the way .css() works
};
})(jQuery);
我只需要扩展 .css()
函数的一些位.请注意我的提问,我在考虑 PHP 类,因为您可以 className 扩展 existingClass
,所以我在问是否可以扩展 jQuery 函数.
There are only a few bits I need to extend of the .css()
function. Mind me for asking, I was thinking about PHP classes since you can className extend existingClass
, so I'm asking if it's possible to extend jQuery functions.
推荐答案
当然...只需保存对现有函数的引用,并调用它:
Sure... Just save a reference to the existing function, and call it:
(function($)
{
// maintain a reference to the existing function
var oldcss = $.fn.css;
// ...before overwriting the jQuery extension point
$.fn.css = function()
{
// original behavior - use function.apply to preserve context
var ret = oldcss.apply(this, arguments);
// stuff I will be extending
// that doesn't affect/change
// the way .css() works
// preserve return value (probably the jQuery object...)
return ret;
};
})(jQuery);
这篇关于扩展现有的 jQuery 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!