如何触发CSS"悬停状态"使用Javascript? [英] How to trigger CSS "hover state" using Javascript?

查看:216
本文介绍了如何触发CSS"悬停状态"使用Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS的徘徊状态,当用户将鼠标悬停在一个元素会触发:

CSS's "hovered state" will trigger when the user hovers over an element:

<style>
.element{

}
.element:hover{
    background-color:red;
}
</style>

我们怎样才能设置元素为国家盘旋使用Javascript?

How can we set the element to "hovered state" using Javascript?

这可能吗?

推荐答案

如果您可以接受使用:聚焦而不是悬停,你可以使用:

If you could accept using :focus instead of hover, you can use:

var links = document.getElementsByTagName('a');
links[0].focus();

JS提琴演示

或者

var linkToFocus = document.getElementById('link');
linkToFocus.focus();

JS提琴演示

这个方法,很明显,需要适应你的CSS包含 A:聚焦风格:

This approach, obviously, requires adapting your CSS to include the a:focus style:

a:link, a:visited {
    background-color: #fff;
}
a:hover, a:focus, a:active {
    background-color: #f00;
}

这篇关于如何触发CSS&QUOT;悬停状态&QUOT;使用Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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