2

我已经看到 jQuery 的插件可以这样扩展:

$.ui.plugin.add('draggable', 'zIndex', {
    start: function(event, ui) { //... }
})

我想知道是否可以扩展扩展名?所以,比如说,我想在可拖动插件的 zIndex 扩展中改变一些东西(不是全部),也就是说,在启动回调函数的最开始放置一些我自己的代码。我该怎么办?如何访问该功能来代理它?

4

1 回答 1

1

沿着这些思路:

(function ($) {
    function proxyPlugin(plugin, callback, option, func) {
        $.each($.ui[plugin].prototype.plugins[callback], function (k, v) {
            if (v[0] == option) {
                var fn = v[1];

                v[1] = function () {
                    func.apply(this, arguments);

                    fn.apply(this, arguments);
                }
            }
        });
    }

    proxyPlugin('draggable', 'start', 'zIndex', function (e, ui) {
        // we are in our custom function that will be triggered before the original one.
        // our custom function receives all the arguments the original one has.
        // for example, let's add a property by the name of foo to the ui object. 
        ui.foo = true;
    });
})(jQuery);
于 2011-08-26T01:06:35.303 回答