JavaScript 和 HTML DOM 參考手冊
js的變量類型有字符串,布爾等
在操作這些變量類型的時候,可以將他們看成是對象來操作
因為js 把一切都封裝成對象來看 獲取字符串的長度
var str = 'hello world';
console.log(str.length); //11
console.log(str.substr(0,5)); // hello
console.log(str); // hello world;
var arr = \['a','b','c','d'\];
console.log(arr.join('~'));//將一個數組拆分為字符串
console.log(str.split(' '));// 將一個字符串分割為數組
String 字符串對象
- length屬性: 長度
- indexOf(string) 返回出現字符串的位置
- substr(num1,[num2]) 截取字符串
- replace(str1,str2) 字符串替換
var str = 'abcdef';
str.indexOf('d') //3
str.substr(0,3) //'abc'
str.replace('a','f') //'fbcdef'
Date 日期對象
- getFullYear() 返回年份(4位)
- getMonth() 返回月份 0-11
- getDate() 返回日期 1-31
- getHours() 返回小時數 0-23
- getMinutes() 返回分鐘數 0-59
- getSeconds() 返回秒數 0-59
var d = new Date();
d.getFullYear()+'-'+d.getMonth()+'-'+d.getDate()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds() //日期YmdHis
Math 數學對象
- ceil(數值) 大于或等于該數的最小整數,即小數
做四舍五入
取整 - floor(數值) 小于或等于該數的最大整數 即小數
不做四舍五入
取整
var num = 3.56;
Math.ceil(num) //4
Math.floor(num) //3