jquery addClass() 不适用于 event.target
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23211129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery addClass() not working with event.target
提问by qtgye
Please help.
请帮忙。
Why is the jquery addClass()
not working with event.target
?
I have made a code and it supposed to add class on the target when clicked, but it does not work, and it says,e.target.addClass
is not a function.
为什么 jqueryaddClass()
不工作event.target
?我已经编写了一个代码,它应该在点击时在目标上添加类,但它不起作用,它说,e.target.addClass
不是一个函数。
CODE:
代码:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Class Test</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<style>
.big {
font-size: 5em;
}
.small {
font-size: 1em;
}
</style>
</head>
<body>
<p>This is the text</p>
<script>
$(function() {
$("p").click(function(e) {
$("a").removeClass("big").addClass("small");
$("this").addClass("big"); //This doesn't work
e.target.addClass("big"); //nor this one
});
});
</script>
</body>
</html>
回答by Rajaprabhu Aravindasamy
Basically e.target will be a javascript
object, you have to convert it into a Jquery
object before utilizing its functions like .addClass()
基本上 e.target 将是一个javascript
对象,您必须Jquery
在使用其功能之前将其转换为对象,例如.addClass()
Try,
尝试,
$(e.target).addClass("big");
and at the same time, $("this").addClass("big");
this code will not work since you are passing this
as a string. this
also a javascript object, you need to convert that too as a jquery object like $(this)
同时,$("this").addClass("big");
此代码将不起作用,因为您是this
作为字符串传递的。this
也是一个 javascript 对象,您也需要将其转换为 jquery 对象,例如$(this)
回答by Bhojendra Rauniyar
You have twoproblem:
你有两个问题:
$(this).addClass("big"); //Unquote to "this"
$(e.target).addClass("big"); // select e.target with $() wrapper.
回答by Gautam3164
Change this line
改变这一行
$("this").addClass("big"); //This doesn't work
to
到
$(this).addClass("big"); //This will work work
And further if you need it with the event
itself then you can use
此外,如果您需要它event
本身,那么您可以使用
$(e.target).addClass('big');
回答by Felix
Since .addClass()
is a jQuery method, you need a jQuery object, so convert e.target
to jQuery object by wrapping it inside $
:
由于.addClass()
是 jQuery 方法,因此您需要一个 jQuery 对象,因此e.target
通过将其包装在内部将其转换为 jQuery 对象$
:
$(e.target).addClass("big");
Beside that, the other solution is to use $(this)
. you don't need to wrap this
inside " "
:
除此之外,另一种解决方案是使用$(this)
. 你不需要包裹this
在里面" "
:
$(this).addClass("big");
回答by u3553434
<script>
$(function() {
$("p").click(function(e) {
$("a").removeClass("big").addClass("small");
$(this).addClass("big"); //This doesn't work
});
});
</script>
"this"=>this
“这个”=>这个
回答by Zeeshan Muhammad
e.currentTarget.classList.add("loading_arrow");
you can use above line to add loading_arrow class using js mouse event.
您可以使用上面的行使用js鼠标事件添加loading_arrow类。