[removed] vs <body onload=“”/>

后端 未结 13 1955
盖世英雄少女心
盖世英雄少女心 2020-11-22 13:45

What exactly is the difference between the window.onload event and the onload event of the body tag? when do I use which and how shoul

13条回答
  •  误落风尘
    2020-11-22 14:19

    Think of onload like any other attribute. On an input box, for example, you could put:

    
    

    Or you could call:

    document.getElementById('test1').value = "somethingelse";
    

    The onload attribute works the same way, except that it takes a function as its value instead of a string like the value attribute does. That also explains why you can "only use one of them" - calling window.onload reassigns the value of the onload attribute for the body tag.

    Also, like others here are saying, usually it is cleaner to keep style and javascript separated from the content of the page, which is why most people advise to use window.onload or like jQuery's ready function.

提交回复
热议问题