Why is using onClick() in HTML a bad practice?

前端 未结 10 2178
我寻月下人不归
我寻月下人不归 2020-11-21 05:07

I have heard many times that using JavaScript events, such as onClick(), in HTML is a bad practice, because it\'s not good for semantics. I would like to know w

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 05:35

    It's a new paradigm called "Unobtrusive JavaScript". The current "web standard" says to separate functionality and presentation.

    It's not really a "bad practice", it's just that most new standards want you to use event listeners instead of in-lining JavaScript.

    Also, this may just be a personal thing, but I think it's much easier to read when you use event listeners, especially if you have more than 1 JavaScript statement you want to run.

提交回复
热议问题