假设我想做一个叫做'alertMeOnRemove‘的小插件,这是不言而喻的。我希望它只覆盖调用它的元素的remove()函数。
$('a').alertMeOnRemove();
$('div').remove(); // no alert
$('a.subset-of-a').remove(); //alert
$('a').remove() //alert我知道一般的重写jQuery函数既可能也很容易。这个是可能的吗?
发布于 2011-01-05 22:39:45
是啊,那并不难。
(function(){
var _remove = $.fn.remove;
$.fn.alertMeOnRemove = function(){
this.each(function(){
$.data(this, 'alertMeOnRemove', true);
});
};
$.fn.remove = function(){
this.each(function(){
if ($.data(this, 'alertMeOnRemove')) {
alert('removing item');
}
});
_remove.call(this);
};
})();发布于 2011-01-05 22:36:08
我不确定我是否能理解你,但你可以这样做:
var _oldRemove = $.fn.remove;
$.fn.remove = function() {
if(/* some condition */)
alert('foobar');
_oldRemove.apply(this, arguments);
};https://stackoverflow.com/questions/4605064
复制相似问题