简体   繁体   English

使用 (this) 在悬停父元素时选择类的特定实例

[英]Using (this) to select a specific instance of a class while hovering a parent element

I'm attempting to use jQuery to update the css of a specific instance of the child "slide-atc" class when the user hovers over its parent element of the "slide-block" class.当用户将鼠标悬停在“slide-block”类的父元素上时,我试图使用 jQuery 来更新子“slide-atc”类的特定实例的 css。

My only restriction is that I cannot edit the HTML directly.我唯一的限制是我不能直接编辑 HTML。 Here is some code that I was trying but I dont think im using .this() correctly这是我正在尝试的一些代码,但我认为我没有正确使用 .this()

 $(".slide-block").hover(function(){ $(this).(".slide-atc").css("bottom", "0px"); }, function(){ $(this).(".slide-atc").css("bottom", "-70px"); });
 <div class="slide-block"> <div class="slide-atc"> </div> </div>

Any help is greatly appreciated!任何帮助是极大的赞赏!

Your code has a syntax error: you will need to chain .find() in order to select the nested child, ie:您的代码有一个语法错误:您需要链接.find()才能选择嵌套的子项,即:

$(this).find(".slide-atc").css("bottom", "0px");

Alternatively, you can provide this as a second argument in a jQuery selector:或者,您可以将this作为 jQuery 选择器中的第二个参数提供:

$(".slide-atc", this).css("bottom", "0px");

Please try using "this" without the $ sign.请尝试使用不带 $ 符号的“this”。 The word "this" is a reference to the html element in the DOM itself that is the event source. “this”这个词是对 DOM 本身中作为事件源的 html 元素的引用。 In the other hand "$(this)" is a jQuery wrapper around that element that enables using other jQuery methods.另一方面,"$(this)" 是围绕该元素的 jQuery 包装器,可以使用其他 jQuery 方法。 Also, I think you have an error at line 4 at as the second parameter is not a background-color.另外,我认为您在第 4 行处有错误,因为第二个参数不是背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM