2011-07-16 56 views

回答

3

你可以把它右出的this

$(selector).click(function() { 
    var id = this.id; 
    // Do interesting things. 
}); 

在jQuery的點擊回調this僅僅是一個DOM對象,公開其屬性作爲對象屬性。

1

可以使用this關鍵詞:

$(".elementClass").click(function() { 
    var currentElement = $(this); // refers to the current element 

    var theId = currentElement.attr("id"); 
}); 
+0

我以爲這是你如何做到這一點,我只是好奇,如果有一個更直接的方式,然後調用attr()函數。 –

+0

這是獲取屬性的方式,因爲你無法調用jquery對象的'element.id'屬性。 – Ibu

+2

但是'this'不是那個上下文中的jQuery對象,它是一個普通的舊DOM對象。 –

相關問題