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

前端 未结 6 1567
礼貌的吻别
礼貌的吻别 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 01:02

    $(document).click(function(e){
        var clickElement = e.target;  // get the dom element clicked.
        var elementClassName = e.target.className;  // get the classname of the element clicked
    });
    

    this supports on clicking anywhere of the page. if the element you clicked doesn't have a class name, it will return null or empty string.

提交回复
热议问题