The basic use of JavaScript and HTML collation, JavaScript use method _php Tutorial

Source: Internet
Author: User
Tags button type

The basic use of JavaScript and HTML to organize, JavaScript use method


JavaScript: Writing HTML output
Instance

document.write ("

This is a heading

");d Ocument.write ("

This is a paragraph

");

Tip: You can only use document.write in HTML output. If you use this method after the document is loaded, the entire document is overwritten.
JavaScript: Responding to events
Instance

Click here

The alert () function is not commonly used in JavaScript, but it is very handy for code testing.
The onclick event is just one of the many events you will learn in this tutorial.
JavaScript: changing HTML content
Using JavaScript to work with HTML content is a powerful feature.
Instance

X=document.getelementbyid ("demo")//Find element x.innerhtml= "Hello JavaScript";  Change Content

You will often see document.getElementById ("some id"). This method is defined in the HTML DOM.
The DOM (Document Object model) is the official standard for accessing HTML elements.


JavaScript: changing HTML styles
Changing the style of HTML elements is a variant of changing HTML attributes.
Instance

X=document.getelementbyid ("demo")//Find element x.style.color= "#ff0000";      Change Style

JavaScript: Validating input
JavaScript is often used to validate user input.
Instance

If IsNaN (x) {alert ("Not Numeric")};

The scripts in the HTML must be between the tags.
Scripts can be placed in and parts of HTML pages.
will tell JavaScript where to start and end.
The line of code between the lines contains JavaScript:


You don't need to understand the code above. Just understand that the browser interprets and executes the JavaScript that is located between.
Those old examples may be in the.

JavaScript Functions and Events
The JavaScript statements in the example above will be executed when the page loads.
In general, we need to execute code when an event occurs, such as when a user taps a button.
If we put JavaScript code in a function, we can call it when the event occurs.
You'll learn more about JavaScript functions and events in a later chapter.
or in the JavaScript
You can put an unlimited number of scripts in the HTML document.
The script can be in the HTML or part, or it may exist in two parts at a time.
The usual practice is to put the function in the section or at the bottom of the page. This allows them to be placed in the same position without disturbing the contents of the page.
The JavaScript functions in
In this example, we put a JavaScript function in the section of the HTML page.
The function is called when the button is clicked:
Instance

My Web Page

A Paragraph

Try it

The JavaScript functions in
In this example, we put a JavaScript function in the section of the HTML page.
The function is called when the button is clicked:
Instance

My Web Page

A Paragraph

Try it

Tip: We put JavaScript at the bottom of the page code to make sure

Executes the script after the element is created.
External JavaScript
You can also save the script to an external file. External files typically contain code that is used by multiple Web pages.
The file name extension for external JavaScript files is. js.
If you want to use an external file, please

It is possible for the

to reference the script file in or. The actual run effect is exactly the same as the script you wrote in the

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.