JavaScript Dom_2 JS Authoring Location

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.