use*_*766 45 html javascript css toggle hover
我有一个<div>,我想在悬停时切换它的类.
这是我的代码:
function a(){
this.classList.toggle('first');
this.classList.toggle('sec');
}
document.querySelector('#container').addEventListener('click', a );
Run Code Online (Sandbox Code Playgroud)
我知道我的HTML或CSS没有问题.只是我必须改变并放置一些东西click,但我不知道是什么.
请帮忙!!
Tom*_*ung 49
悬停事件称为"mouseenter"而不是"click".
<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>
Run Code Online (Sandbox Code Playgroud)
虽然仲镇华的方法肯定能行,最好给mouseenter和mouseleave自己的处理程序:
var container = document.querySelector('#container');
container.addEventListener('mouseenter', function(){
this.classList.remove('first');
this.classList.add('second');
})
container.addEventListener('mouseleave', function(){
this.classList.add('first');
this.classList.remove('second');
})Run Code Online (Sandbox Code Playgroud)
.first {
background: green;
}
.second {
background: orange;
}Run Code Online (Sandbox Code Playgroud)
<div id="container" class="first">
TEST
</div>Run Code Online (Sandbox Code Playgroud)
(另见小提琴)
| 归档时间: |
|
| 查看次数: |
71492 次 |
| 最近记录: |