Date()获取当前时间
<button onclick="this.innerHTML=Date()">现在的时间是?</button>
转换为更加易读的格式
Date().toDateString()
其他时间用法 https://www.w3school.com.cn/js/js_dates.asp
返回字符串长度
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
查找目标字符串中的字符串在第几位
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
更多方法见 https://www.w3school.com.cn/js/js_string_methods.asp
四舍五入获取数字
var x = 9.656;
x.toFixed(0); // 返回 10
x.toFixed(2); // 返回 9.66
x.toFixed(4); // 返回 9.6560
x.toFixed(6); // 返回 9.656000
把看似数字的字符变成数字的方法
Number("10")
查看字符类型
document.write(typeof "Bill");
返回0到9之间的随机整数
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10);
</script>
把value变成大写
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
每多少秒执行一次的函数,循环执行
setInterval
参考:https://www.runoob.com/jsref/met-win-setinterval.html
延迟几秒执行的方法
setTimeout("location.reload()",5000);
setTimeout(function () {
// 这里就是处理的事件
}, 2000);
setTimeout(function () {document.getElementById('loadding').style.display = 'block';}, 1500);