[英]“this” not working as expected
I've been struggling with this for an hour now, and I can't solve it. 我一直在努力奋斗一个小时,我无法解决它。 I'm new to use this
on javascript, but this is really simple, and it's just not working. 我是新手在javascript上使用this
,但这很简单,而且它不起作用。
Here's the HTML 这是HTML
<ul class="nav pull-right nav-tabs" id="primarynav">
<li class=" navlink">
<a href="#">About</a>
</li>
<li class="navlink active">
<a href="#">Portfolio</a>
</li>
<li class="navlink">
<a href="#">Contact</a>
</li>
</ul>
And the js 和js
$(document).ready(function(){
$(".navlink").click(function(){
$(".navlink").removeClass("active");
this.addClass("active");
});
});
So it should remove class active
from all elements with class navlink
, and then add class active
to the clicked element. 因此,它应该使用类navlink
从所有元素中删除active
类,然后将class active
添加到单击的元素。 But it doesn't. 但事实并非如此。
Change it to: 将其更改为:
$(this).addClass("active");
.addClass()
is a jQuery method and you had been trying to apply it to a non-jQuery object ( this
vs $(this)
). .addClass()
是一个jQuery方法,你一直在尝试将它应用于非jQuery对象( this
与vs $(this)
)。
addClass() is a jQuery method but this
is just the direct reference to the DOM object. addClass()是一个jQuery的方法,但this
仅仅是直接引用DOM对象。 You need to wrap this
into a jQuery object first before you can use a jQuery method on it. 你需要用this
成jQuery对象之前,首先你可以在上面使用jQuery的方法。
$(document).ready(function(){
$(".navlink").click(function(){
$(".navlink").removeClass("active");
$(this).addClass("active");
});
});
DEMO - Using $(this).addClass()
instead of this.addClass()
演示 - 使用$(this).addClass()
而不是this.addClass()
Edit 编辑
To elaborate a little on this. 详细说明一下。 You can never call jQuery's addClass()
method on a JavaScript object as addClass()
is a jQuery method. 你永远不能在JavaScript对象上调用jQuery的addClass()
方法,因为addClass()
是一个jQuery方法。
To do the same in pure JavaScript, if you want to just use this
, you could use element.className , similar to this: 要在纯JavaScript中执行相同的操作,如果您只想使用this
,可以使用element.className ,类似于:
$(document).ready(function(){
$(".navlink").click(function(){
$(".navlink").removeClass("active");
//$(this).addClass("active");
this.className += " active";
});
});
DEMO - Using this.className
example 演示 - 使用this.className
示例
Though if you are using jQuery already it would make little sense not to use $(this).addClass()
instead. 虽然如果你已经使用jQuery,那么不使用$(this).addClass()
就没有意义了。
this
refers to the DOMElement to which jQuery has attached the event. this
是指jQuery附加事件的DOMElement。 To turn it into a jQuery collection and be able to use jQuery methods like addClass
, pass it as an argument to $
: 要将其转换为jQuery集合并能够使用addClass
等jQuery方法,请将其作为参数传递给$
:
$(this).addClass("active");
Inside functions, the this
keyword actually refers to the context of the function. 在函数内部, this
关键字实际上是指函数的上下文。 In the case of event handlers, the context is the DOMElement that the handler is attached to. 对于事件处理程序,上下文是处理程序附加到的DOMElement。
this
不是一个jQuery对象,而是使用$(this)
。
You should use $(this) 你应该使用$(this)
$(document).ready(function(){
$(".navlink").click(function(){
$(".navlink").removeClass("active");
$(this).addClass("active");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.