开发者

jQuery, 'denyClass' extension sought after

I would like to write a jQuery 'plugin' that esse开发者_运维问答ntially accepts a CSS class and denies it from ever being added to a given selector. Is this even possible? I cannot figure out how to actually intercept the addClass method and check it without going into the jQuery source code - and my experience with javascript is not nearly strong enough to be comfortable trying that. Plus I am loading jQuery through a CDN, so I'd rather not muck around in its source.


You could overwrite it like this if you accept hardcoding denied classes. You can add this anywhere, it will simply 'patch' jQuery with an enhanced addClass function.

http://jsfiddle.net/pimvdb/A8ePY/1/

(function($) {

// keep copy of original function
var old = $.fn.addClass;

$.fn.addClass = function(classes) {
    var spl = classes.split(" ");

    // remove denied class if present
    if(spl.indexOf("denied") > -1) {
        spl.splice(spl.indexOf("denied"), 1);
    }

    // pass everything to original function (altered classes plus other arguments)
    return old.apply(this,
                     spl.concat([].slice.call(arguments, 1)));
};

})(jQuery);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜