简体   繁体   English

如何让jQuery只修改一个div,而不是同一个类的所有div?

[英]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.

相关问题 Javascript:querySelector 只选择 1 个 div。 如何将其发送到 select 所有具有相同 class 的 div? - Javascript: querySelector only selects 1 div. How do I get it to select all divs with the same class? 如何在HTML中修改所有选定类的子div? - How do I modify all of a selected class' child divs, in HTML? 如何使这个jQuery div滚动条在同一个类的多个div上工作? - How can I make this jQuery div scroller work on multiple divs with the same class? 如何使用jQuery影响同一类的8个div的单个div? - How can I affect a single div of 8 divs of a same class with jQuery? 如何让这个“轮播”显示三个 div 而不是一个? - How do I make this "carousel" show three divs instead of one? 在JS / JQuery中打开1 div关闭同一类(不包括它自己)的所有其他div? - Make opening 1 div close all other divs of same class (excluding itself) in JS/JQuery? 如果其他div有相同的class,如何输入一个div的样式并修改它? - How to enter the style of a div and modify it if other divs have the same class? 仅翻转一个DIV,而不是全部都翻转 - Flip only one DIV and not all of the same class jQuery仅将鼠标悬停在一个div上,但为所有div定义 - jquery hover only to one div but defined for all divs jQuery-尝试使一个函数与多个相同的类,div一起使用 - jQuery - Trying to make one function to work with multiple, same class, divs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM