I have this HTML structure:
$("li").on("click", function(){
// I need to remove active-class form current element
$(this).addClass("active");
});
li:hover{
background-color: #eee;
cursor: pointer;
}
.active{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Numbers:
<ul>
<li>One</li>
<li class="active">Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
<li>
标签的内容时,将会为该元素添加active
类。现在我想检测哪个元素上有active
类并从它那里先移除该类(.removeClass()
),然后再将active
类添加到点击的元素上。换句话说,我需要使它(active
类)变得唯一。我如何做到这一点?