jQuery 使用 onclick 从元素中获取类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17060971/
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-26 18:21:11  来源:igfitidea点击:

get class name from element with onclick

javascriptjqueryonclick

提问by caramba

Because of different reasons I can NOT use $('.class').click or on('click', function..)

由于不同的原因,我不能使用 $('.class').click 或 on('click', function..)

So I really have to use the onclick="" event from the html element.

所以我真的必须使用来自 html 元素的 onclick="" 事件。

Is there a way to find out the class from the element where the onclick happens?

有没有办法从 onclick 发生的元素中找出类?

and the fiddle http://jsfiddle.net/Aw8Rb/

和小提琴http://jsfiddle.net/Aw8Rb/

Thanks for all the help!

感谢所有的帮助!

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>

<span class="classic one"   onclick="someFunction('one')">CLICK HERE</span>
<span class="classic two"   onclick="someFunction('two')">CLICK HERE</span>
<span class="classic three" onclick="someFunction('three')">CLICK HERE</span>
<span class="classic four"  onclick="someFunction('four')">CLICK HERE</span>


<script type="text/javascript">

    function someFunction(abc) {
        alert(abc);
        alert($(this).attr('class'));

    }

</script>
</body>
</html>

回答by bipen

you need to pass thisreference when calling the function

this调用函数时需要传递引用

try this

尝试这个

<span class="classic one"   onclick="someFunction(this,'one')">CLICK HERE</span>
......  //same for others

jquery

查询

function someFunction(obj,abc) {
        alert(abc);
        alert($(obj).attr('class'));

    }

with plain javascript (without Jquery)

使用普通的 javascript(没有 Jquery)

function someFunction(obj,abc) {
        alert(obj.className);
}

fiddle here

在这里摆弄

回答by bipen

A non-jquery way using this.classNamehere.

使用this.classNamehere 的非 jquery 方式。

回答by Alessandro Minoccheri

If you add the element to the argument you can use it into your function to retrieve all data that you want
Try this:

如果您将元素添加到参数中,您可以在函数中使用它来检索您想要的所有数据
试试这个:

    <span class="classic one"   onclick="someFunction('one',this)">CLICK HERE</span>
    <span class="classic two"   onclick="someFunction('two',this)">CLICK HERE</span>
    <span class="classic three" onclick="someFunction('three',this)">CLICK HERE</span>
    <span class="classic four"  onclick="someFunction('four',this)">CLICK HERE</span>


    <script type="text/javascript">

        function someFunction(abc,obj) {
            alert(abc);
            alert($(obj).attr('class'));

        }
    </script>

DEMO

演示

回答by NullPointer

From jsfiddleyou provided, edit your code like below and you'll get answer (class) properly.

根据jsfiddle您提供的内容,编辑您的代码,如下所示,您将正确获得答案(课程)。

<span class="classic four"  onclick="someFunction(this,'four')">CLICK HERE</span>

function someFunction(obj,abc) {
    alert(abc);
    alert(obj.getAttribute('class'));   
}

Pass thisas first argument and then access it in function with obj. After that you can use obj.getAttribute('class')

this作为第一个参数传递,然后在函数中使用obj. 之后你可以使用obj.getAttribute('class')

回答by Kevin Bowersox

Add thisas an argument to the inline event handler:

添加this作为内联事件处理程序的参数:

<span class="classic one"   onclick="someFunction(this,'one')">CLICK HERE</span>

Then use the classNameproperty of element to get the className

然后使用className元素的属性来获取className

function someFunction(e,abc) {
      console.log(this);
      alert(e.className);
}

Working Examplehttp://jsfiddle.net/Aw8Rb/8/

工作示例http://jsfiddle.net/Aw8Rb/8/

回答by Jason

Pass this.classNameas the parameter .. eg.

this.className作为参数传递.. 例如。

<input type="button" onclick="abc(this.className)" />

回答by Sudip Pal

onclick="javascript:someFunction(this)"

function someFunction(obj) {  
            console.log($(obj).attr('class'));  
}

You will get the two classes in the order of writing.

你会按照写作的顺序得到这两个类。