2009-01-01 41 views
2

我的網站的每個頁面都有10個(幾乎)相同的div,只在文本內部變化。當點擊div的某個部分時,我想要修改該div的不同部分。如何讓jQuery只修改一個div,而不是同一類的所有div?

我使用這個代碼:

$(document).ready(function(){ 
$(".notLogged img").mouseover(function(){ 
    $(this).parent()>$("span").html("You must be logged in to vote."); 
}) 
}) 

我想這會工作如下:對於「notLogged」歸類專區內的所有img元素,鼠標懸停會造成該分區的不同部分改變。

但是,這不是什麼情況。每當鼠標懸停事件觸發時,全部 divs與「notLogged」類都被修改。

我該如何修改此代碼,以便只修改鼠標懸停的div?

+0

您可以發佈您的HTML? – Greg 2009-01-01 19:14:02

回答

8

試試這樣說:

$(document).ready(function(){ 
$(".notLogged img").mouseover(function(){ 
    $(this).parent().find("span").html("You must be logged in to vote."); 
}); 
}); 
+1

美麗。很有幫助。 – stalepretzel 2009-01-01 19:19:29

+0

很高興能有所幫助。 – 2009-01-01 19:20:08

0
// Assuming the 1st span in the div is the one to be modified. 
$(this).parent().$('span:eq(1)').html("You must be logged in to vote."); 
// :eq(1) selects the first element that matches its selector. You could 
// also use ':first' in this case. 
相關問題