JS Preliminary usage

Source: Internet
Author: User
Tags script tag

Js
JS Introduction Method:
1. Mode One
Introduced through the script tag

2. Mode II
By introducing the script tag, the SRC attribute introduces an external JS file

Note: If you use the SRC attribute of the script tag, then the JS code in its tag body does not work.
Grammar:
1.ECMAScript Syntax specification
LiveScript
2.BOM Browser Object Model

3.DOM Document Object Model

1. Variable definition:
var I=1;
var m;
JS also has a comment that says:
Single-line comment//
Multi-line Comment/**/
Statement end semicolon End can not write suggestion write on

2. Data type
Original type:
Undefined: Variable initial value if the function does not return a value, you receive it.
String: Use "" to cause
Number: Value
Boolean:true|false
Null:null a placeholder for an object
The typeof operator determines the data type of the current variable if it is the original type that returns the type value of the data type (lowercase) if it does not return an object

Reference type:
New Object ();
3. Control statements
Basically consistent with Java.
If for
4. Operators
Basically consistent with Java.
Relational operators
> >= < <=
1. Compare both sides if all are number types consistent with Java
2. Compare both sides are ordinary strings it will compare the Ascci code of both
3. The comparison between the two parties is a number that can be converted into a number-tired implicit conversion
4. Compare the two sides is the number party is non-conversion string directly return False


Equal-sex operators
==
===
= = Compare two variable literals to return True
= = = Determines whether the type is the same on the same basis as the comparison literal

5. Functions
Defined
Way One
Function name (argument list) {
JS code block
}
Way two anonymous functions
var f=function (parameter list) {
}
Attention:
Calling functions
Function name (parameter list);
Parameter declaration procedure parameter list not write type
If you want to have a return value you only need to return the result with the return keyword at the end of the function

6. Events
Form submission Events
Click event onclick
Page Load Success Event onload
Event Bindings
Way One
Bind by the event property of the tag
Way Two
Distribution mechanism
Element. Event name =function () {

}

var Obj=document.getelementbyid ("ID");

Timer:
Defined:
Way One
SetInterval (the function name, the millisecond value of the periodic interval);
Way Two
SetInterval ("Function name ()", millisecond value of the periodic interval);
Mode three
SetInterval (function () {
JS code block
}, the millisecond value of the periodic interval);
Two types of timers

The function defined by the timer will have a return value that is its ID


1. Periodic timers
var intervalid=setinterval (function name, periodic interval millisecond value)
2. Disposable Timer
var timeoutid=settimeout (function name, millisecond value of delay);

To clear the timer:

1. Clear the periodic timer
Clearinterval (the ID of the periodic timer);
2. Clear the Disposable timer
Cleartimeoout (ID of the disposable timer);

BOM Browser Object Model
Window:
If you want to get a Window property or method window can not write

Common Properties:
Use it to get several other common properties
var history=window.history;
History===window.history
Common methods:
Pop-Up box class:
Alert ("message");//pop-up warning box
Confirm ("Are you sure to delete");//Popup A confirmation box
Prompt ("Input message information", default);//pop-up an input box if you are sure to return your input value if NULL is canceled
Timer:
Open and Close:
Open (URL);
Close ();

History:
Forward: Forward a page
Back: Rewind a page
Go (1);
1: Forward a page
-1: Back one page


Location:
HREF: Set value jump
DOM Operations
Get element
var Obj=document.getelementbyid ("");
Get value
Obj.value
Setting the value
Obj.value= value
Get tag Body content
obj.innerhtml;
Set up
Obj.innerhtml= value
Validating data with regular expressions
1. Writing Regular Expressions
1. Regular user name: Var reg=/^[a-z0-9_-]{3,16}$/
2. Password:/^[a-z0-9_-]{6,18}$/
3. E-mail:/^ ([a-z0-9_\.-]+) @ ([\da-z\.-]+) \. ([A-z\.] {2,6}) $/
4. Empty string:/^\s*$/
2. Using the regular expression's test () method
Reg.test (value);
Summary of events:
Must master:
OnSubmit: Form Submission Event
OnClick: Click event
OnLoad: Page Load Success Event
Onfocus: Get Focus
Onblur: Lost Focus
Need to know:
Associated with Mouse:
OnMouseUp: Mouse Bouncing up
OnMouseDown: mouse button down
onmouseover: Mouse hover
onMouseOut: Mouse Away
OnMouseMove: Mouse Movement

OnDblClick: Mouse Double-click
Related to keyboard:
OnKeyUp: Press the key to bounce up
OnKeyDown: Press the button
onkeypress: Press the button and bounce
Related to the form:
OnReset: Resetting form events
onchange: Changing Events
Dom
Document Object Model
Document node
ELEMENT node:
Attribute node:
Text node
Query operation:
Get element:
1.document.getelementbyid ("x");//Gets the label's ID property value is a label of X
2.document.getelementsbytagname ("Y");//Get a set of labels named Y (multiple)
3.document.getelementsbyname ("z");//Gets the label of a group of labels with the name attribute Z
4.document.getelementsbyclassname ("w");//Gets the label's class attribute as a set of W tags (multiple)
Get Value:
Obj.value
Set the value:
Obj.value= value
Get the contents of a tag body
obj.innerhtml;
To set the label body:
Obj.innerhtml= "<a href= ' # > Point I'll try </a>";
Get style
Obj.style.css Property Name: If any-remove the camel-named
Set up
Obj.style.css Property name =css property value
Extensions-Other APIs:
Document
CreateAttribute (name): Create an attribute node
CreateElement (): Create an element node
createTextNode (data): Create a text node
Element
AppendChild (): Add child nodes
Array:
Grammar:
var arr1=new array ();//Create an empty array
var arr2=new array (size);//Create an array of a specified length
var arr3=new Array (Ele1,ele2,ele3 ...); /create an array and initialize the data
Attention:
There is no variable length in the JS array
JS array can be stored in different types

Property:
Length: Long
Arr.length
Common methods:
Join ();//Stitch a string with the specified style character

Adding data to the head of an array
Unshift ();
Delete data from the head of an array
Shift ();

Add data to the end of the array
Push ()
Delete data from the end of an array
Pop ()

Will change the structure of the array
Sort ();
Reverse ();
The reference type of JS
1.Array: Array

The original type string Boolean number in JS
String Boolean number in reference type
The original type of the wrapper class called the primitive type. method ();
2.String:
New String (s);//object typeof operator Object
String (s);//It is the original type
Common Properties:
Length: Long
Common methods:
SUBSTRING (startindex,endindex); [)
Slice (startindex,endindex);
Endindex: The value can be negative
-1: The countdown to the first one
Split (); Cut string
Replace (regular expression)
Match (regular expression);

3.Number
New number ();//object
Number ();//Primitive type
4.Boolean
New Boolean ();//object
Boolean ();//Primitive type
5.REGEXP:
/Direct Volume notation/
Test ()
6.Math:
Math.PI
Math.random ();
Math.Round ();
7.Date
var date=new date ();
Common methods:
GetTime ();
toLocaleString ()
8. Global functions
Attempt to convert to int integer value
parseint ();
Attempt to convert to float floating point number
Parsefloat ();

Cast numeric type
Number ();
Cast to String
String ();

encodeURI (): Encoding
decodeURI (): Decoding

Eval ()//computes the JavaScript string and executes it as script code.

JS Preliminary usage

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.