How can i rewrite this function as below:
$('#TblInvoiceList td img.ImgDelete').live('click' , function () {
开发者_如何学Python $(this).parent().parent().remove();
});
Desired form:
$('#TblInvoiceList td img.ImgDelete').live('click' , function () {
delete(this);
});
delete: function(){
$(this).parent().parent().remove();
}
To set the value of this
in a function use Function.call()
, i.e.:
delete.call(this);
The first parameter to the call()
and apply()
methods set the context (i.e. the value of this
) of the called function.
Alternatively, if your listener only has to call delete
then write your event listener thus:
$('#TblInvoiceList td img.ImgDelete').live('click' , delete);
since there's no need for a new closure just to invoke delete
.
$('#TblInvoiceList td img.ImgDelete').live('click' , delete);
var delete = function(){
$(this).parent().parent().remove();
}
Just pass in the function directly so that this
is scoped properly.
精彩评论