扩展现有的jQuery函数 [英] Extending an existing jQuery function

查看:93
本文介绍了扩展现有的jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (function($)
{
$ .fn.css = function()
{
//我会延长
//不影响/更改
// .css()的工作方式为
};
})(jQuery);

我只需要扩展 .css( )函数。因为你可以 className extends existingClass ,所以我问是否可以扩展jQuery函数。

解决方案

当然,请保存对现有函数的引用,然后调用它:

<$ ($($)
{
//保持对现有函数的引用
var oldcss = $ .fn.css;
// ...覆盖jQuery扩展点之前
$ .fn.css = function()
{
//原始行为 - 使用function.apply保存上下文
var ret = oldcss.apply(this,arguments);

//我会延长
//不影响/改变
//方式.css( )工作

//保存返回值(可能是jQuery对象...)
return ret;
};
})(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);

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函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆