简体   繁体   中英

Add onclick event to div in js

I'm trying to add a onclick event to a tag, but it isn't working. Can anyone tell me the correct way to add an onclick to this tag in js?

  var cell = result.appendChild(document.createElement('abbr'));
  cell.title = "cell number 1";
  cell.className = 'chart-cell-type'; 
  cell.addEventListener='onclick',(alert("clicked!"));  //broken                                                      

As Alexis points out, your syntax is wrong. You want something like:

cell.addEventListener("click", function() {alert("clicked");})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM