2009-11-13 13 views
0

內選擇考慮以下幾點: -jQuery的 - 點擊方法

$("#id").click(function(){ 
// how do i access $("#id") here: say i want to add a class to #id? without $("#id") 
}); 

回答

4

可以使用this上下文來訪問選擇的項目。

$("#id").click(function() { 
    $(this).addClass("blah"); 
}); 

另外,每個事件處理程序也將傳遞一個事件,你可以得到你從的信息:

$("#id").click(function(evt) { 
    $(evt.target).addClass("blah"); 
}); 

jQuery Event object

但是,我通常贊成使用this的方法。

+0

Bah。到10秒。 :p – nlaq 2009-11-13 09:26:03

0
$(this).addClass("class");