开发者

send this as argument of function in jquery

开发者 https://www.devze.com 2023-03-04 02:43 出处:网络
How can i rewrite this function as below: $(\'#TblInvoiceList td img.ImgDelete\').live(\'click\' , function () {

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.

0

精彩评论

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