JavaScript click event listener on class

后端 未结 5 1038
悲&欢浪女
悲&欢浪女 2020-11-22 10:19

I\'m currently trying to write some JavaScript to get the attribute of the class that has been clicked. I know that to do this the correct way, I should use an event listene

5条回答
  •  悲&欢浪女
    2020-11-22 11:01

    You can use the code below:

    document.body.addEventListener('click', function (evt) {
        if (evt.target.className === 'databox') {
            alert(this)
        }
    }, false);
    

提交回复
热议问题