开发者

jQuery core method overload

开发者 https://www.devze.com 2022-12-24 07:10 出处:网络
how can 开发者_高级运维I overload or extend (or intercept) jQuery\'s .html() method so that it works its default way unless the object has a certain class?

how can 开发者_高级运维I overload or extend (or intercept) jQuery's .html() method so that it works its default way unless the object has a certain class?

Thanks


You just overwrite the function while maintaining a reference to the original.

var jq_html_function = $.fn.html;

$.fn.html = function() {
    $(this).each(function() {
        if($(this).hasClass('someclass')) {
            // Do something
            return;
        }
        jq_html_function.apply(this, arguments);
    });
};


Isn't that bit of a hacky solution? I think we should let the framework be as it is. What if you wanted to use new version of jQuery, or if you hired new developer who needed to use unmodified .html() ?

Why not:

$('.blah').setHtml('<div>something</div>');

Where:

(function($)
{
    $.fn.setHtml = function(html)
    {
        if (html.parse_for_class == true)
        {
            //do something with it
        }
        else 
        {
            $(this).html(html);
        }
        return;
    }
}
0

精彩评论

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