A combination of JavaScript and HTML explain _javascript techniques

Source: Internet
Author: User
Tags button type

JavaScript scripts in HTML must be located between the <script> and </script> tags, javascript scripts can be placed in the <body> tags of HTML pages and First, <script> label
To insert a JavaScript script into an HTML page, use the <script> tab. <script> and </script> will tell JavaScript where to start
and end. The lines of code between <script> and </script> contain javascript:

<span style= "FONT-SIZE:18PX;" ><script type= "Text/javascript" > 
alert ("Welcome to the JavaScript world!!! "); 

You don't need to understand the code above. Just understand that the browser interprets and executes JavaScript between <script> and </script>. Those old
The old instance may use Type= "Text/javascript" in the <script> tag. There is no need to do so now. JavaScript is all modern browsers
and the default scripting language in HTML5. In view of just learning JavaScript language can be used!
second, the JavaScript in <body>
In this example, JavaScript writes text to HTML <body> when the page loads:
Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

Let's first look at the results of the run, regardless of how the JavaScript code is written and how it works:

Third, JavaScript functions and events
The JavaScript statement in the example above is executed when the page is loaded. Typically, we need to execute code when an event occurs, such as when the user
When you click the button. If we put JavaScript code into a function, we can call that function when the event occurs.

Four,
You can put an unlimited number of scripts in an HTML document. Scripts can be located in the <body> or In two sections. The usual practice is to put the function into the Does not interfere with the content of the page.

Five, the JavaScript function in
In this case, we put a JavaScript function into the Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

The results of the run are:

Click the button after the effect is:

Vi. the Javascrip function in <body>
In this case, we put a JavaScript function into the <body> section of the HTML page. The function is invoked when the button is clicked:
Instance code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

The results of the        run are the same as those of the above five!
       Hint: We put JavaScript at the bottom of the page code so we can make sure that the <p After the > element is created, the script is executed.
Seven, external JavaScript
       We can also save the script to an external file. External files typically contain code that is used by multiple Web pages. File extension for external JavaScript files
The exhibition name is. js. To use an external file, set the. js file in the "src" attribute of the <script> label, and if there is a lot of JavaScript code, I
advocate the use of an external JavaScript method, In general, we also connect to HTML documents in a separate way.
Instance
HTML code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

Myscript.js Code:

function MyFunction () 
{ 
document.getElementById ("Demo"). Innerhtml= "My I-JavaScript Function"; 
} 

Run the results consistent with the above!
Tip: referencing script files in External scripts cannot contain <script> tags.

The above is the combination of JavaScript and HTML method, I hope to help you learn.

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.