Add click event on div tag using javascript

前端 未结 6 1619
别那么骄傲
别那么骄傲 2020-12-03 02:46

I have a div tag in my form without id property. I need to set an on-click event on this div tag.

My HTML code:

6条回答
  •  借酒劲吻你
    2020-12-03 03:09

    Recommend you to use Id, as Id is associated to only one element while class name may link to more than one element causing confusion to add event to element.

    try if you really want to use class:

     document.getElementsByClassName('drill_cursor')[0].onclick = function(){alert('1');};
    

    or you may assign function in html itself:

提交回复
热议问题