Reference

网页的动作语言

Examples are better than 1000 words.——一例胜千言

引用

<head></head>

<body></body>

<script></script>

<script type="text/javascript" src="javascript.js"></script>

输出

  • innerHTML

document.getElementById("demo").innerHTML='Hello World!';
  • document.write()

  • window.alert()

  • console.log()

  • print()

控制

  • getElementById("demo")

  • getElementsByClassName("demo")

  • getElementsByTagName("p")

  • querySelectorAll("p.intro")

声明

  • var

  • let

  • cost

注释

  • //单行注释

  • /*多行注释*/

运算符

  • +

  • -

  • *

  • **

  • /

  • %

  • ++

  • --

  • &&

  • ||

  • !

  • ==

  • ===

  • !=

  • !==

  • >

  • <

  • >=

  • <=

  • ?

  • +=

  • -=

  • *=

  • /=

  • %=

  • **=

函数

function myFunction(p1, p2) {
return p1 * p2; // The function returns the product of p1 and p2
}
  • 数组:x = ["a","b","c"]

  • 物体:x={name:"fiat",model:"modern",color:"white"}

字符串处理

  • .length

  • indexOf()

  • lastIndexOf()

  • search()

  • slice()

  • substring()

  • substr()

  • replace()

  • toUpperCase()

  • toLowerCase()

  • concat()

  • trim()

  • charAt()

  • charCodeAt()

  • split()

数字

Js语法中,支持数位是15位,以及小数点后17位。

NaN = not a number

isNaN()

数字进制

var myNumber = 32;
myNumber.toString(10); // returns 32
myNumber.toString(32); // returns 10
myNumber.toString(16); // returns 20
myNumber.toString(8); // returns 40
myNumber.toString(2); // returns 100000

数字变成物体

var x = new Number(124);
  • toExponential()

  • toFixed()

  • toPrecision()

数组

var cars = ["Saab", "Volvo", "BMW"];||var cars = new Array("Saab", "Volvo", "BMW");
  • array.foreach()

  • array.push()

  • array.pop()

  • typeof.array

  • array.join("*")

  • array.shift()

  • array.unshift()

  • delete array[0]

  • array.splice(2,0,"apple","orange")

  • array.concat(array)

  • sort()

  • reverse()

调用

person.lastName;||person["lastName"];

时间

  • Date(year, month, day, hours, minutes, seconds, milliseconds)

  • toUTCString()

  • toISOString()

  • toDateString()

getFullYear()

Get the year as a four digit number (yyyy)

getMonth()

Get the month as a number (0-11)

getDate()

Get the day as a number (1-31)

getHours()

Get the hour (0-23)

getMinutes()

Get the minute (0-59)

getSeconds()

Get the second (0-59)

getMilliseconds()

Get the millisecond (0-999)

getTime()

Get the time (milliseconds since January 1, 1970)

getDay()

Get the weekday as a number (0-6)

Date.now()

Get the time. ECMAScript 5.

Method

Description

getUTCDate()

Same as getDate(), but returns the UTC date

getUTCDay()

Same as getDay(), but returns the UTC day

getUTCFullYear()

Same as getFullYear(), but returns the UTC year

getUTCHours()

Same as getHours(), but returns the UTC hour

getUTCMilliseconds()

Same as getMilliseconds(), but returns the UTC milliseconds

getUTCMinutes()

Same as getMinutes(), but returns the UTC minutes

getUTCMonth()

Same as getMonth(), but returns the UTC month

getUTCSeconds()

Same as getSeconds(), but returns the UTC seconds

Method

Description

setDate()

Set the day as a number (1-31)

setFullYear()

Set the year (optionally month and day)

setHours()

Set the hour (0-23)

setMilliseconds()

Set the milliseconds (0-999)

setMinutes()

Set the minutes (0-59)

setMonth()

Set the month (0-11)

setSeconds()

Set the seconds (0-59)

setTime()

Set the time (milliseconds since January 1, 1970)

数学

  • Math.PI

  • Math.round()

  • Math.pow()

  • Math.sqrt()

  • Math.abs()

  • Math.ceil()

  • Math.floor()

  • Math.sin()

  • Math.cos()

  • Math.min()

  • Math.max()

  • Math.random()

  • Math.E

  • Math.SQRT2

  • Math.SQRT1_2

  • Math.LN2

  • Math.LN10

  • Math.LOG2E

  • Math.LOG10E

逻辑

逻辑

True or False

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
  • Boolean()

  • variablename = (condition) ? value1:value2

if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
switch (new Date().getDay()) {
case 6:
text = "Today is Saturday";
break;
case 0:
text = "Today is Sunday";
break;
default:
text = "Looking forward to the Weekend";
}