[英]How do I make jQuery modify only one div, instead of all divs of the same class?
Each page of my site has 10 (almost) identical divs, varying only in the text within. 我网站的每个页面都有10个(几乎)相同的div,只在其中的文本中有所不同。 When a certain part of a div is clicked, I want a different part of that div to be modified. 当单击div的某个部分时,我希望修改该div的不同部分。
I'm using this code: 我正在使用此代码:
$(document).ready(function(){
$(".notLogged img").mouseover(function(){
$(this).parent()>$("span").html("You must be logged in to vote.");
})
})
I thought this would work as follows: For all img
elements within a "notLogged" classed div, a mouseover would cause a different part of that div to change. 我认为这将按如下方式工作:对于“notLogged”类别div中的所有img
元素,鼠标悬停将导致该div的不同部分发生变化。
However, that's not what happens. 然而,事情并非如此。 Whenever that mouseover event is triggered, all divs with the "notLogged" class are modified. 每当触发鼠标悬停事件时, 都会修改具有“notLogged”类的所有 div。
How can I modify this code so only the div in which the mouseover originated is modified? 如何修改此代码,以便仅修改鼠标悬停所在的div?
Try it like this: 试试这样:
$(document).ready(function(){
$(".notLogged img").mouseover(function(){
$(this).parent().find("span").html("You must be logged in to vote.");
});
});
// 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.