JavaScript & Events - Best Practice

后端 未结 4 939
星月不相逢
星月不相逢 2020-12-19 11:17

I once read that the following coding technique is considered bad:

text link
<         


        
4条回答
  •  情话喂你
    2020-12-19 11:50

    Not sure of the official term, but it's not good practice because HTML should be pure markup, without mixing client side script directly into it.

    Best practice is attaching the event in such way:

    window.onload = function() {
        document.getElementById("MyLink").onclick = function() {
            alert('Hello World');
            return false;
        }
    }
    

    After giving the link ID MyLink for example.

提交回复
热议问题