JS Code Writing Location:
1. Write in the head node. As shown in the following code:
2. Write within the HTML tag.
<body> <button onclick= "alert (' Hello world! ')" >ClickMe</button></body>
Description: This is not suitable for writing JS code, because the HTML code and JSD code coupling, if the JS code is larger than the time should not be used in this way, not conducive to the management of code, but also not easy to write. When the code is small, this way can be easily written to achieve the effect and function, so to eradicate the size of the project and the amount of code, reasonable choice of this way, but it is recommended not to use this way, not conducive to late expansion.
3. Writing outside the HTML node
<HTML> <HEAD> <TITLE>JavaScript</TITLE> </HEAD> <BODY> </body> ;
4. Write the introduction in a separate file where it is used
var btn = document.getelementsbytagname ("button") [0]; Btn.onclick = function () {alert ("Hello everyone!"); }
This approach is recommended because decoupling HTML and JavaScript code, while facilitating later maintenance and extension, is a method that many programmers use.
Summarize:
The specific use of which or which mixed use, are based on the needs of the project, but all the purpose is to reduce the cost of pre-and post-maintenance, so there is no way to use the constraints, but the fourth is the most common.
This article is from the "Love Coffee" blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1569082
JavaScript Dom_2 JS Authoring Location