使用js触发css:hover事件 [英] Trigger the css:hover event with js

查看:228
本文介绍了使用js触发css:hover事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有< div class =animate> 和css:

div.animate:hover{
//do stuff
}


$ b

But would also like to invoke this via javascript.

是否可以?

推荐答案

而不是这样做,我建议你只是添加一个类到其他标签。在jQuery中它将是:

Instead of doing it this way, I suggest you just add a class to the other tag. In jQuery it would be:

 $(window).load(function() {
    $('.trigger-animate').hover(function(){
        $('.animate').addClass('hover');
    });
}

我建议使用这个方法,因为它处理onMouseOver和onMouseOut(这种方式你也可以删除类,当你的鼠标离开$ ('.trigger-animate')如果你希望使用这种语法:

I'd recommend using this method, because it handles both onMouseOver and onMouseOut (this way you can also remove the class when your mouse leaves $('.trigger-animate') if you so desired using this syntax:

.hover( handlerIn(eventObject), handlerOut(eventObject) )
 checking out the documentation

这篇关于使用js触发css:hover事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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