当mouseenter在不同元素上时如何应用和删除类? [英] How to apply and remove class when mouseenter on different element?
本文介绍了当mouseenter在不同元素上时如何应用和删除类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我的编码仅在mouseenter中添加类,但在mouseenter其他元素时从不删除活动类。
我尝试过:
查看下面的编码:
$( document )。ready( function (){
$( #1,#2,#3)。mouseenter( function (){
$( this )。removeClass( active)
$( # + this .id)。addClass( active);
});
});
.active {
background:#fff ;
}
< pre > < < span class =code-leadattribute> ul >
< li > < a class = active id = 1 > 数字1 < / a > < / li >
< li > < a id = 2 > number 2 < / a > < / li >
< li > < a id = 3 > number 3 < span class =code-keyword>< / a > < / li >
< span class =code-keyword>< / ul >
解决方案
( document )。ready( function (){
( #1,#2,#3)。mouseenter( function (){
( this )。removeClass( 活性跨度>)
So far my coding is working only adding the class when mouseenter but never remove the active class when mouseenter other element.
What I have tried:
Check out my coding below :
$(document).ready(function() {
$("#1, #2, #3").mouseenter(function() {
$(this).removeClass("active")
$("#" + this.id).addClass("active");
});
});
.active { background: #fff; }
<pre><ul>
<li><a class="active" id="1">number 1</a></li>
<li><a id="2">number 2</a></li>
<li><a id="3">number 3</a></li>
</ul>
解决方案
(document).ready(function() {
("#1, #2, #3").mouseenter(function() {
(this).removeClass("active")
这篇关于当mouseenter在不同元素上时如何应用和删除类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文