onclick=“” vs event handler

前端 未结 8 2293
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 00:20

If I want a function to be executed, I prefer doing inline js:

Click me

becaus

8条回答
  •  深忆病人
    2020-11-22 00:48

    Apart from the coding standards perspective

    Using Attributes:

    Click me

    if you add the same attribute again (see below) -the latest one is considered.

    Click me

    Using Event Handler:

    document.getElementById('element').onclick = doSomething;

    and lets say you added the below line

    document.getElementById('element').onclick = doSomethingMore; Both the handlers are invoked.

提交回复
热议问题