This article mainly introduces the sequence of JavaScript execution, and a friend in need can refer to the
JavaScript is the highest order of execution, unless you specifically specify that JavaScript code does not wait until the page is loaded. For example, a Web page contains the following HTML code: Code as follows: <div id= "Ele" >welcome to www.jb51.net</div> If you are in this line of HTML code before, Add the following JavaScript code: Code as follows: <script type= "Text/javascript" > document.getElementById (' Ele '). Innerhtml= ' Welcome to my blog '; </script> Run the page, you will get the error message: "document.getElementById (' ele ') is null. "The reason is that when the above JavaScript is running, there is no DOM element with the id ' ele ' on the page. There are two ways to solve this problem: 1. Put the JavaScript code behind the HTML code: the code is as follows: <div id= "Ele" >welcome to www.jb51.net</div> <script " Text/javascript "> document.getElementById (' Ele '). Innerhtml= ' Welcome to my blog '; </script> 2. Wait until the Web page has finished loading to run the JavaScript code. You can use the traditional solution (load): First add HTML to the "<body load=" "Load ()" "," and then invoke the above JavaScript code in the load () function. Here's what you can focus on with jquery: The code is: <script> $ (document). Ready (function () { document.getelementbyid (' ele ') . innerhtml= ' Welcome to my blog '; }); </script> of course, since the use of jquery, a simpler formulation is: <script> $(document). Ready (function () { $ (' #ele '). HTML (' Welcome to my blog ');//This is also available. Text () method}); </script> You can put the jquery code above anywhere on the page, and it will always wait until the page has finished loading.