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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:12:34  来源:igfitidea点击:

jquery addClass() not working with event.target

jqueryeventstargetaddclass

提问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.addClassis not a function.

为什么 jqueryaddClass()不工作event.target?我已经编写了一个代码,它应该在点击时在目标上添加类,但它不起作用,它说,e.target.addClass不是一个函数。

http://jsfiddle.net/Lq9G4/

http://jsfiddle.net/Lq9G4/

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 javascriptobject, you have to convert it into a Jqueryobject 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 thisas a string. thisalso 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 eventitself 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.targetto 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 thisinside " ":

除此之外,另一种解决方案是使用$(this). 你不需要包裹this在里面" "

$(this).addClass("big");

Updated Fiddle

更新的小提琴

回答by Milind Anantwar

Try this:

尝试这个:

 $(e.target).addClass('big');

Demo

演示

回答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类。