2009-01-01 35 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. 
相关问题