2015.7.7js-07-2 (Basic)

Source: Internet
Author: User

1. Use modulo to calculate the time, seconds%60, you can get the remaining seconds

var s = 362;

var minute = parseint (s/60) + "min"//Get Points

var second = s%60 + "SEC"//Get seconds

var time = minute + second; 6 minutes, 2 seconds.

2. Ternary operators

var a = 12;

if (a%2 = = 0) {

Alert ("even");

}else{
Alert ("singular");

}

Condition? Statement 1 (TRUE): statement 2 (FALSE);

A%2==0?alert ("even"): alert ("singular");

3.json

var json ={a:12,b:5,c: "ABC"};

JSON does not have length, so the loop is used for the (var i in Fson);

for (var i in JSON) {

Alert ("+i+"-something: "+ json[i]);

}

4. Take non-inline style (only a single style, cannot take compound style such as: background)

1.IE:OBJ.CURRENTSTYLE[ATTR];

2.chrome,firefox:getcomputedstyle (Obj,false) [attr];

Packaging:

function GetStyle (obj,attr) {

if (Obj.currentstyle) {

alert (obj.currentstyle[attr);

}else{

Alert (getComputedStyle (Obj,false) [attr];

}

}

5. Arrays

var arr = [1,2,3,4,5];

1. Length can be used to set it

arr.length = 0; Empty array

2. Adding and removing arrays

Push (), added from the tail, Arr.push (6);

Unshift (), added from the head, Arr.unshift (6);

Delete

Pop (), removed from tail, arr.pop (); No parameters

Shift (), remove Arr.shift () from the head; No parameters

Splice is added at any location in the array, deleting

1.splice (beginning, length) Delete, Arr.splice (2,2); The

Insert

2.splice (starting point, 0, "a", "B")//starting at the second position insert a, a B [1,2,a,b,3,4,5]

Replace

3.splice (Start, length, add Element) Arr.splice (2,2, "a", "B"), remove 2 from Start 2, then add 2, equals replace

6.sort Array Sorting

var arr = [1,10,3,59,5,20];

function sort (num1,num2) {

if (Num1 < num2) {

return-1;

}else if (Num1 > Num2) {

return 1;

}else{
return 0;

}

}

2015.7.7js-07-2 (Basic)

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.