JS Basics (ii)-12.10

Source: Internet
Author: User

(1) The display of JS date (date):

Output Current time:

var m=new date (); ( where the "D" of date must be capitalized )

Console.log (m);


(2) The timer in JS:

x.setinterval ("JS statement", time) ( where time is 11 milliseconds as unit, 1000 milliseconds is 1 seconds );

X.settimeout ("JS statement", time);

X.clearinterval ("JS statement", time) stop timer;


(3) The String object in JS:

1.

CharAt () Returns the character at the specified position.

Where 0 is the first value Eg:charat (4) outputs a fourth character value.

2.

Concat () Connect 2 or more strings.

3.

fromCharCode () Creates a string from character encoding.

Eg:alert (String,fromcharcode ());

4.

IndexOf (, start) Retrieves a string.

Returns the position of the first occurrence of a character in a string, or 1 if it is not found. Start can freely define where to start looking.

5.

Slice (start,end) Extracts a fragment of a string and returns the extracted part in a new string.

Start is the start position and end is the terminating position.

6.

Split ()

Splits a string into an array of strings.


(4) Array object:

(1) Create an array: var x=new arrey[];

(2) The properties of the array:

Pop (): Deletes the last value of the array and returns the first one.

Shift (): Deletes the first element.

Push () adds one or more arrays to the end of the array.

Unshift () adds one or more elements to the beginning of the array header.

Splice () Adds or removes elements to the array. Syntax: Splice (index,how many,item1,,itemx), where index indicates the location to add or remove, how many is 0 to add, if greater than 0 means the number of deletions, not written means all deleted. New represents the element item that was added.

Reverse () The array in reverse order.

The Join ("") conversion array is a string.

Split () Converts the string to an array.

Sort () sorts the array elements. function Softnumber (A, A, a, b) {return a-B}; This item is sorted in ascending order. The bubble sort can also be used to sort the array in curly brackets.


(5) Node properties;

1.

The GetAttribute () method returns the value of the property.

The GetAttributeNode () method returns the attribute node.

2. Get child nodes:

X.childnodes[num], access the NUM+1 child node under the parent node X (calculates an empty node, a bug occurs when there are spaces).

X.children[num], access the NUM+1 child node under the parent node X (calculates an empty node, a bug occurs when there are spaces).

Firstelementchild, does not contain the first child node.

Lastelemnetchild, which does not contain the last child node.

3. Get the parent element:

X.panentnode.nodename (Gets the node name of the parent node X)

X.panentnode.panentnode.nodename (get x grandfather node)

4. Sibling elements:

Previoussibling,previouselementsibling (Gets the previous sibling element, which contains an empty node);

Nextsibling,nextelementsibling (Gets the latter sibling element, which contains an empty node);

5, OffsetParent look for the first parent node with a positional attribute, return <BODY> if not found;.

6, X.appendchild () The node name is added after the parent node X, Documnet.createelement () creates a new element.

7, removechild () Delete a node, in parentheses fill in the name of the node that needs to be deleted.

8, ReplaceChild ("new element", "position of old element").


(6) Ways to block time bubbling:

Event.stoppropagation (); Stop bubbling.

Event.preventdefault (); block default events.

Add javascript:void (0) to the <a> tab to block the default event for hyperlinks.

return false; either block the default event or block the bubbling event.


(7) Monitoring events (support multiple monitoring of events to avoid time conflicts):

Syntax: Element.addeventlistener ("click", function{}); all trigger event names do not contain on.



JS Basics (ii)-12.10

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.