Hook into an onClick event without using the HTML property

后端 未结 3 1102
终归单人心
终归单人心 2021-01-02 17:33

I would like to keep my JavaScript and HTML code separate. To do this, I want to make sure that I never use the following syntax:



        
3条回答
  •  温柔的废话
    2021-01-02 17:50

    If you give your element and ID, you can do:

    var el = document.getElementById("text");
    el.addEventListener("click", function(/*put code here*/) {}, false);
    

提交回复
热议问题