Code base for JavaScript

Source: Internet
Author: User

code base for JavaScript

This article is mainly a collection of some JavaScript in some of the frequently used code. Convenient for later search and reuse.

JavaScript Framework:

<script language= "javascript" type= "Text/javascript" >
JavaScript code
</script>

the definition of a function in JS:

Function name ()

{
function body
}

three major structures:1. Sequential Structure:
2. Select the structure:

if (the result is an expression of type Boolean)

{
The statement that is run when the expression is true.
}
Else

{
The statement that is run when the expression is false.
}

3. Cyclic structure

Loop Initial value
while (loop condition)

{
JavaScript statements that need to be looped
Change the Loop initial value
}


For (initial value of the loop; loop condition; change loop initial value)

{
JavaScript statements that need to be looped
}

operator

Arithmetic operators: +,-, *,/,% (remainder), = (Assignment)

Arithmetic operators:>, >=, <, <=,! =, = =

Logical operators: && () and, | | (OR),! (Take the reverse)

+ =,-=, *=,/=,%=

+ + (self-add 1) 、--(self-minus 1)

//Array

var a=new Array ();
a[0]=5;
a[1]=9;
a[2]=2;
A.sort (); Sort from small to large
Alert ("Length of the array:" +a.length);
Alert (a[0]+ "," +a[1]+ "," +a[2]);

Escape characters:line break: \ n

The form is:. Write between the double slash//. Number of references outside the second slash
. G = Global Lookup (g is global)
. I means ignoring uppercase and lowercase (i is an abbreviation for ignore case)
Match a series of whitespace characters:/^\s+$/g
Match the left and right whitespace characters of the string:/(^\s+) | (\s+$)/g
Match a series of Chinese:/^[\u4e00-\u9f5a]+$/g
Matching numbers can have decimal points:/^\d+ (\.\d+)? $/g
QQ Number 5 digits above/^\d{5,}$/g//Note the curly braces {} are entered here. Not parentheses ().

Email format verification:/\w+ ([-+.] \w+) *@ (\w+ ([-.] \w+) *\.\w+) */g


Other:

var x = document. Table sole name. text box name. Value; The value in the text box exists in the variable x
Document. Table sole name. text box name. Select (); Select all the contents of the text box.
Document. Table sole name. text box name. focus (); The text box gets the focus.


Alert ("Good people"); Popup dialog box

prompt ("Please enter password", "Default value"); Pop-up input box


event : onfocus gets focus onblur loses focus when the onclick Click event onchange drop-down menu changes.

Submit Form : document. Table sole name. Submit ();

get elements by ID : document.getElementById ("id")

The code that is often used in JavaScript is now a collection of these, and many other frequently used code is collected later.


Code base for JavaScript

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.