jQuery core method overload
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;
}
}
精彩评论