qtg*_*gye 19 jquery events addclass target
请帮忙.
为什么jquery addClass()不能使用event.target?我已经制作了一个代码,并且它应该在单击时在目标上添加类,但它不起作用,并且它说,e.target.addClass它不是一个函数.
码:
<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>
Run Code Online (Sandbox Code Playgroud)
Raj*_*amy 38
基本上e.target将是一个javascript对象,你必须Jquery在使用它的函数之前将它转换为一个对象.addClass()
尝试,
$(e.target).addClass("big");
Run Code Online (Sandbox Code Playgroud)
同时,$("this").addClass("big");由于您this作为字符串传递,此代码将无法工作.this也是一个javascript对象,你需要将它转换为像jquery对象一样$(this)