如何在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>