在悬停#leaderboard时,向#左箭头和#右箭头添加一个类 [英] On hovering #leaderboard, add a class to #left-arrow and #right-arrow

查看:95
本文介绍了在悬停#leaderboard时,向#左箭头和#右箭头添加一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这样一个基本的问题,但我需要知道如何向两个div添加一个类,#left-arrow和#right-arrow。



我明白我可以使用call hover(),但我不确定如何实现它以在另一个元素上添加一个类。



我绝对是Javascript的开始,所以更具体的如何实现它更好!谢谢!



-Judson

解决方案

您可以使用 .toggleClass() ,如下所示:

  $(#leaderboard)。hover(function(){
$(left-arrow,#right-arrow ).toggleClass(myClass);
});

这会在您悬停时添加该类,并在鼠标离开时将其删除。如果您想 添加该类,请 .addClass () 将可以正常工作,您可以使用 <$ c
$ b


$ b $。


我明白我可以使用call hover(),但我不确定如何实现它以在另一个元素上添加一个类。




只要使用像上面那样的选择器,您不限于在中使用 this 任何功能,您可以使用选择器遍历函数在适当的位置移动。


Sorry for such a basic question, but I need to know how to add a class to two divs, #left-arrow and #right-arrow when I hoverover #leaderboard.

I understand I can use the call hover(), but I'm unsure how to implement it to add a class on another element.

I'm definitely a beginning with Javascript, so the more specific on how to implement it the better! Thanks!

-Judson

解决方案

You can use .toggleClass(), like this:

$("#leaderboard").hover(function() {
  $("left-arrow, #right-arrow").toggleClass("myClass");
});

This adds the class when you hover, removed it when the mouse leaves. If you want to just add the class, then .addClass() will work and you can manually remove it with .removeCass() later.

For this part:

I understand I can use the call hover(), but I'm unsure how to implement it to add a class on another element.

Just use a selector like I have above, you're not restricted to using this inside any function, you can use a selector or traverse functions to move around wherever appropriate.

这篇关于在悬停#leaderboard时,向#左箭头和#右箭头添加一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆