如何在HTML中使用纯javascript切换类 [英] How to toggle class using pure javascript in html

查看:114
本文介绍了如何在HTML中使用纯javascript切换类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< div> ,我想在悬停时切换它的类。

I have a <div>, and I want to toggle its classes on hover.

是我的代码:

function a(){
    this.classList.toggle('first');
    this.classList.toggle('sec');
}
document.querySelector('#container').addEventListener('click', a );

我知道我的html或css没有问题。这只是我必须改变,并放置一些点击,但我不知道什么。

I know there is no problem in my html or css. It is just that I have to change and put something in place of click, but I don't know what.

悬浮事件称为mouseenter。

Please help!!

推荐答案

而不是点击。

<script type="text/javascript">
    function a(){
        this.classList.toggle('first');
        this.classList.toggle('sec');
    }
    document.querySelector('#container').addEventListener('mouseenter', a )
    document.querySelector('#container').addEventListener('mouseleave', a )
</script>

这篇关于如何在HTML中使用纯javascript切换类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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