Basic javaScript explanation and javascript explanation

Source: Internet
Author: User

Basic javaScript explanation and javascript explanation

The placement of javaScript: <script> and </script> can be placed between head and body, or between body or head.

Six major functions of JavaScript:

1. output directly in script

Document. write ("

2. Respond to the event

<Button type = "button" onclick = "alert ('Welcome! ') "> // Click here </button> // onclick indicates that when a click event is triggered, the alert dialog box appears.

3. Change HTML content

X = document. getElementById ("demo") // search for an element and obtain an element by id. Just as my previous note said that the id can only be used once, so it is unique. X. innerHTML = "Hello JavaScript"; // Changes the content. The value of this element changes when an event is triggered.

4. Change the HTML Image

Element = document. getElementById ('myimage') // obtain the img element. src = "/I/eg_bulboff.gif"; // changing the Src attribute is equivalent to changing an image.

5. Change the HTML Style

X = document. getElementById ("demo") // find the element x. style. color = "# ff0000" by id; // change the style

6. Verification Input

If (isNaN (x) {alert ("Not Numeric")}; // isNaN () is a method used to determine whether all digits in brackets are true.

Three common methods for searching elements in javaScript (excluding jquery)

1: search by id;

X = document. getElementById ("demo") // query by id is unique, and x represents a unique element.

2: search by name:

X = document. getElementsByName ("myInput"); // here, the ments indicates that it returns a set, because the name attribute can be repeatedly defined as X [0]. innerHTML = "Hello JavaScript"; // specify the first few because it is an array. Even if there is only one, it must be written to zero.

3: search by element name:

Var x = document. getElementsByTagName ("input"); // also returns an array alert (x. length); // here there are several input labels to output several JavaScript comments, which are the same as those in java. // and /**/

JavaScript Variables

JavaScript data includes strings, numbers, Boolean values, arrays, objects, Null values, and Undefined values.

Var cars = new Array (); cars [0] = "Audi"; // Array expression var cars = ["Audi", "BMW", "Volvo"]; // It is also an array expression person = new Object (); // Object expression

JavaScript Functions

Function getElements () {}// if you can include a parameter, pass the parameter (var1, var2) to it. If you want to return a return value, the function type remains unchanged.

JavaScript operators, Arithmetic Operators, value assignment operators, comparison operators, logical operators, conditional operators, IF statements, Switch statements, For loops, while do-while, break and Continue are the same as java rules (a little different is for (x in person ))

Next we will make a few small cases:

1: the account name is verified, and the two passwords are consistent.

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html> 

The running result is as follows:

2: Implement an addition Calculator

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html> 

The running result is as follows:

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.