如何在JavaScript中编写hoverHandler来在悬停时改变元素的颜色?
function hoverHandler(e)
{
if(event.target.getAttribute("id") != "hovering")
{
event.target.setAttribute("id", "hovering");
}
}
这是我的代码,我还有一个 CSS,可以在 id 悬停时设置颜色。
问题: 1)当我悬停时,当我离开元素时,颜色不会重置回以前的颜色
function hoverHandler(e)
{
if(event.target.getAttribute("id") != "hovering")
{
event.target.setAttribute("id", "hovering");
}
}
This is the code I have, I also have a CSS that sets the color when id is hovering.
The problem:
1) As I am hovering, the color does not get reset back to previous color when I leave the element
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
难道直接用CSS就可以解决问题吗?
一样
就像元素是类名
Can't you just use Css to solve the problem?
Something like
where element is the class name
尝试添加,并确保您也检查了
onmouseout
,或者内联事件处理程序可能会将
onmouseout
默认返回基本样式(如果有);或者try adding, and make sure your checking for
onmouseout
as wellor an inline event handlers could be
onmouseout
would default back to the base style if any; or