2017-04-20 103 views
0

我試圖訪問$(this)作爲回調傳遞給另一個函數。

我已經閱讀了很多類似的問題,但只是無法找到答案。這是代理進入還是綁定?

我提供的下面是我想做一個超級簡單的例子:

function my_function(callback){ 

    // Do Something 
    callback(); 

} 


$('.el').click(function(){ 

    my_function(function(){ 
    $(this).hide(); // How to access $(this) in the callback? 
    }); 

}); 

回答

0

試試這個,

$('.el').click(function(){ 
    var el = this; // assign this to el 
    my_function(function(){ 
    $(el).hide(); // use el here 
    }); 
}); 
+0

聖耶穌那個快啊 - 和它的作品! – Chris

+0

太棒了,它的作品:D。 - Jai Shri Ram。 –