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

前端 未结 10 2145
我寻月下人不归
我寻月下人不归 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条回答
  •  温柔的废话
    2020-11-21 05:33

    You're probably talking about unobtrusive Javascript, which would look like this:

    link
    

    with the logic in a central javascript file looking something like this:

    $('#someLink').click(function(){
        popup('/map/', 300, 300, 'map'); 
        return false;
    });
    

    The advantages are

    • behaviour (Javascript) is separated from presentation (HTML)
    • no mixing of languages
    • you're using a javascript framework like jQuery that can handle most cross-browser issues for you
    • You can add behaviour to a lot of HTML elements at once without code duplication

提交回复
热议问题