从具有OnClick属性的元素获取类名

9
因为某些原因,我不能使用 $('.class').click 或 on('click', function..)。
所以我必须使用来自HTML元素的 onclick="" 事件。
有没有办法找出发生 onclick 的元素的 class?
还有一个 JSFiddle http://jsfiddle.net/Aw8Rb/
感谢您的所有帮助!
<!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>

切换解决方案对我有用 - https://dev59.com/6HVC5IYBdhLWcg3wvT7g#196038 - Manohar Reddy Poreddy
8个回答

25
你在调用函数时需要传递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'));

    }

使用纯JavaScript(无需jQuery)

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

这里有一个 fiddle


谢谢!它帮了我很多。干杯,伙计 :) - Anoop Thiruonam
它可以工作,但是它也获取了内部类,除了给定的类之外。那么有没有办法只获取类名而不包括内部类呢? - Kunal Tyagi
@KunalTyagi 很抱歉回复晚了,但你无法这样做,因为className或者.attr('class')会给出关联到该元素的所有类。你可以遍历这些类并获取/检查所需的一个。最重要的是,实际上取决于你获取类之后需要什么,遍历它并执行必要的操作。 愉快编码! - bipen

5

这是一个不使用jQuery的方法,使用this.className 在这里查看示例。


2
如果你将元素添加到参数中,就可以在函数中使用它来检索所有想要的数据。
试一下这个:
    <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


2

您提供的 jsfiddle,请按照以下方式编辑您的代码,您将正确地获得答案(类)。

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

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

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


2
this.className作为参数传递..例如。
<input type="button" onclick="abc(this.className)" />

1
this作为内联事件处理程序的参数添加:
<span class="classic one"   onclick="someFunction(this,'one')">CLICK HERE</span>

然后使用元素的 className 属性来获取类名。
function someFunction(e,abc) {
      console.log(this);
      alert(e.className);
}

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


0
你可以像这样做:
js...
function thisFunction(e) {
  console.log(e.target.class); // should return the class of the element that called it (button)
}
html...
<button onclick="thisFunction()">Click Me</button>

0
onclick="javascript:someFunction(this)"

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

你将按照编写顺序获取这两个类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接