[英]Change class of li element using Jquery
I have a navigation which highlights the currently selected navigation point by using the class active. 我有一个导航,通过使用活动类突出显示当前选定的导航点。 When a user visits a new navigation point, i want to toggle of the "old" selected navigation point and toggle the class of the li element of the new navigation point to "active".
当用户访问新导航点时,我想切换“旧”选定导航点并将新导航点的li元素的类切换为“活动”。
I have problems with the jquery part, which only should change toggle of the li class of the old element and adds the class to the clicked li element. 我有jquery部分的问题,它只应该更改旧元素的li类的切换,并将类添加到单击的li元素。 Probably a standard situation, but I don't know a good and easy solution for this.
可能是标准情况,但我不知道这是一个好的和简单的解决方案。
Example (before click): 示例(点击前):
<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>
Result (after click on "Dashboard2"): 结果(点击“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>
My try 我的尝试
$(".x-navigation li").on("click", "a", function(){
event.preventDefault();
//alert("test");
($this).parent().removeClass("active");
($this).addClass("active");
//alert($this.child().href);
});
Error description: 错误说明:
Uncaught ReferenceError: $this is not defined at this row: "($this).parent().removeClass("active");
Shouldn't be $this the li element which got clicked ? 不应该是$ this被点击的li元素? I hope you guys can help me with such a standard problem.
我希望你们能帮我解决这个标准问题。
The $
goes outside the parens. 在
$
而来的括号内 。 The dollar sign is the jQuery constructor call that your are providing this
to as an argument: 美元符号是jQuery的构造函数调用,你所提供
this
作为参数:
$(this).parent()
and so on. 等等。
your question is telling me you want to toggle the class of the li element of the new navigation point to "active" , meaning you want the class "active" to be placed on the li of the link you just clicked. 你的问题是告诉我你要将新导航点的li元素的类切换为“active”,这意味着你希望将类“active”放在你刚刚点击的链接的li上。
but what your doing here is 但你在这做什么呢
($this).addClass("active");
is that your adding the class active to your anchor element instead because of this 是因为这样你将类激活添加到你的锚元素
$(".x-navigation li").on("click", "a", function(){
also this part : 这一部分:
($this).parent().removeClass("active");
should be like this, place the $ outside of (this): 应该像这样,将$放在(this)之外:
$(this).parent().removeClass("active");
now if you really want to toggle it you can simple do this: 现在,如果你真的想要切换它,你可以简单地做到这一点:
$(".x-navigation li").on("click", "a", function(){
event.preventDefault();
$(this).parent().addClass("active").siblings().removeClass("active");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.