Javascript/jQuery - How do I obtain name of the class of clicked element?

前端 未结 6 1563
礼貌的吻别
礼貌的吻别 2020-12-16 00:27

I googled and googled and I concluded that it\'s very hard to get answer on my own.

I am trying to use jquery or JavaScript to get a property of clicked element. I c

6条回答
  •  一整个雨季
    2020-12-16 00:55

    This example will work on every element in the page. I'd recommend using console.log(event) and poking around at what it dumps into your console with Firebug/Developer tools.

    jQuery

    ​$(window).click(function(e) {
        console.log(e); // then e.srcElement.className has the class
    });​​​​
    

    Javascript

    window.onclick = function(e) {
        console.log(e); // then e.srcElement.className has the class
    }​
    

    Try it out

    http://jsfiddle.net/M2Wvp/

    Edit
    For clarification, you don't have to log console for the e.srcElement.className to have the class, hopefully that doesn't confuse anyone. It's meant to show that within the function, that will have the class name.

提交回复
热议问题