Javascript and Anchor Tags, Best Practice?

后端 未结 5 663
梦如初夏
梦如初夏 2021-01-04 02:17

Quick Question.

Should we put Javascript in the HREF or use onClick (event)?

Are there any pros/cons to using either one. Personally I think it\'s easier/cle

5条回答
  •  别那么骄傲
    2021-01-04 03:22

    Keeping the code separate from the html is cleaner IMO.

    Click Me
    

    Then in head or separate js file:

    document.getElementByID("foo").onclick = function() { alert("hi"); }
    

提交回复
热议问题