字符串方法
- 截取字符串
- substr(开始下标,截取的长度)
- substring(开始下标,结束下标)
- slice(开始下标,结束下标)
- 查找字符串首次出现的索引位置
- indexOf()
- 去除前后空格
- trim()
- 转小写
- toLowerCase()
- 转大写
- toUpperCase()
Math方法
console.log(Math.random());// [0,1) 包括0不包括1
// var num = -1;
// console.log(Math.abs(num)); // 获取绝对值的
// var num = 10.2;
// console.log(Math.round(num)); // 四舍五入
// var num = 10.8;
// console.log(Math.ceil(num)); //向上取整
// console.log(Math.floor(num)); //向下取整
// var num = 10;
// console.log(Math.pow(10,2)); // 10的2次方 两个10相乘 100
// console.log(Math.pow(10,3)); // 10的3次方 三个10相乘 1000
// var num = 9;
// console.log(Math.sqrt(9)); // 取跟号 3
// console.log(Math.max(12,45,6)); // 取最大值
// console.log(Math.min(12,45,6)); // 取最小值
日期对象
创建对象
- var date = new Date(); 创建当前本地日期对象
- var date = new Date(‘2020-09-09 10:00:00’); 创建指定的日期对象
对象的方法:
-
toLocaleString() 将本地转为字符串形式展示
// var date = new Date(); // console.log(date.toLocaleString()); // 转为字符串形式表示
-
getTime() 获取距离1970年1月1日 的毫秒值
// var date = new Date(); // 当前本地时间 // var date1 = new Date('2021-05-17 11:00:00'); // var cha = date.getTime() - date1.getTime();// 毫秒值 // console.log(cha); // 毫秒 // console.log(cha/1000); // 秒 // console.log(cha/1000/60); // 分钟 // console.log(cha/1000/60/60); // 小时 // console.log(cha/1000/60/60/24); // 天 // console.log(date.getTime()); // 距离1970年1月1日的毫秒值 // console.log(parseInt(date.getTime()/1000)); // 距离1970年1月1日的秒值
-
获取年,月,日
var date = new Date();
console.log(date.toLocaleString());获取本地时间// console.log(date.getFullYear()); // 获取四位数字的年份 // console.log(date.getMonth() + 1); // 获取四位数字的月份 [0-11] // console.log(date.getDate()); // 获取四位数字的日 // console.log(date.getHours()); // 获取四位数字的小时 // console.log(date.getMinutes()); // 获取四位数字的分钟 // console.log(date.getSeconds()); // 获取四位数字的秒 // console.log(date.getDay()); // 获取星期 0-6 0:星期日
-
补零操作
h = h<10?'0'+h:h; f = f<10?'0'+f:f; s = s<10?'0'+s:s;
-
获取六位十六进制颜色,制作彩虹条
var color = "#"; var str = "0123456789abcdef"; //无论几个都是数组 var list = document.getElementsByTagName("li"); for(var i=0; i<list.length; i++){ color = "#"; for(var j=0; j<6; j++){ color += str.charAt(rand(0,15)); } list[i].style.backgroundColor = color; }
定时器
-
定时器写法: setInterval(回调函数,毫秒值)
setInterval(function(){ // 将页面的内容设置成你的倒计时 document.body.innerHTML = getCha() },1000)
-
倒计时
function getCha() { var date1 = new Date() var date2 = new Date('2021-6-18 00:00:00') var cha = date2.getTime() - date1.getTime(); var s = parseInt(cha / 1000 % 60); //还剩多少秒 var m = parseInt(cha / 1000 / 60 % 60); // 还剩多少分钟 var h = parseInt(cha / 1000 / 60 / 60 % 24); // 还剩多少小时 return '距离618还剩' + h + '小时' + m + '分钟' + s + '秒' }
-
页面加入样式
document.body.style.background = -
div加入样式
oDiv.style.background =