我有一个导航,通过使用类active来突出显示当前选定的导航点。当用户访问新的导航点时,我想切换“旧”选定的导航点,并将新导航点的li元素的类切换为“active”。
我遇到了jquery部分的问题,它只应该更改旧元素的li类的切换状态,并将类添加到单击的li元素中。这可能是标准情况,但我不知道一个好的和简单的解决方案。
示例(点击之前):
错误描述:
"我希望 $this 是被点击的 li 元素,这是一个常见问题,希望你们能够帮助我。"
我遇到了jquery部分的问题,它只应该更改旧元素的li类的切换状态,并将类添加到单击的li元素中。这可能是标准情况,但我不知道一个好的和简单的解决方案。
示例(点击之前):
<ul class="x-navigation">
<li class="active">
<a href="index.php"><span class="fa fa-desktop"></span> <span class="xn-text">Dashboard</span></a>
</li>
<li>
<a href="index.php"><span class="fa fa-desktop"></span> <span class="xn-text">Dashboard2</span></a>
</li>
</ul>
结果(点击“Dashboard2”后):
<ul class="x-navigation">
<li>
<a href="index.php"><span class="fa fa-desktop"></span> <span class="xn-text">Dashboard</span></a>
</li>
<li class="active">
<a href="index.php"><span class="fa fa-desktop"></span> <span class="xn-text">Dashboard2</span></a>
</li>
</ul>
我的尝试
$(".x-navigation li").on("click", "a", function(){
event.preventDefault();
//alert("test");
($this).parent().removeClass("active");
($this).addClass("active");
//alert($this.child().href);
});
错误描述:
Uncaught ReferenceError: $this is not defined at this row: "($this).parent().removeClass("active");
"我希望 $this 是被点击的 li 元素,这是一个常见问题,希望你们能够帮助我。"