好程序員web前端教程:Math函數javascript
Math.round(3.6) //四捨五入html
random() //返回0-1之間的隨機數前端
max(num1, num2) //返回較大的數java
min(num1, num2) //返回較小的數程序員
abs(num) //絕對值web
ceil(19.3) //12 向上取整bash
floor(11.8) //11 向下取整dom
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" /> <title>無標題文檔</title> <script type="text/javascript"> window.onload = function() { var arr = ['a', 'b', 'c'] var oBtn = document.getElementById('btn'); var oTxt = document.getElementById('txt'); oBtn.onclick = function() { var iNum = parseInt(Math.random() * 10000) while (iNum < 1000) { iNum = parseInt(Math.random() * 10000) } oTxt.value = iNum } } </script> </head> <body> <input type="button" value="獲取驗證碼" id="btn" /> <input type="text" value="5345" id="txt" /> </body> </html> 複製代碼
二、驗證函數
<!DOCTYPE HTML>
<html>
<head>
<title>註冊頁面</title>
<meta charset="utf-8" />
<script type="text/javascript">
//能夠對輸入的文本作簡單校驗
function btnClick() {
var oUsername = document.getElementById("box_username");
var oPassname = document.getElementById("box_password");
var oSpan = document.getElementById("box_span");
//清楚掉兩個文本中的空格
var sUsername = oUsername.value.replace(/ /g, "");
var sPassname = oPassname.value.replace(/ /g, "");
//取出用戶名第一個字符
var unChar = sUsername.charAt(0);
if (unChar >= '0' && unChar <= '9') {
oSpan.innerHTML = "用戶名首字母不能夠是數字!";
} else if (sUsername.length == 0) {
oSpan.innerHTML = "用戶名不能爲空!";
} else if (sUsername.length < 6 || sUsername.length > 20) {
oSpan.innerHTML = "用戶名長度應該不小於6而且不大於20!";
} else if (sPassname.length == 0) {
oSpan.innerHTML = "密碼不能爲空!";
}
//將處理完空格的文本替換掉原來錯誤的文本
//替換文本框中的值
oUsername.value = sUsername;
oPassname.value = sPassname;
}
</script>
</head>
<body>
<span id="box_span"></span>
<input id="box_username" type="text" placeholder="用戶名" onblur="btnClick()" />
<input id="box_password" type="password" placeholder="密碼" onblur="btnClick()" />
<button type="button" onClick="btnClick()">登陸</button>
</body>
</html>
複製代碼
思路:佈局
佈局(略)
1.獲取須要操做的元素;
2.增長事件;
3.獲取須要驗證的字符串;
4.對字符串進行處理(1.用戶名不能爲空 。2.開頭字母不能是數字。3.用戶名長度應該小於6位可是不能大於20位。4.密碼不能爲空。);
5.驗證字符串,並將結果寫入提示區域內;
注:空格也是字符;
3.計算器, 計算器思路。
佈局。(略)
switch()語句和if相似,可是比if更復雜;
switch(sth){
case sth1:
代碼塊;
break;
default:
代碼塊;
}
複製代碼
一個小的計算器:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
window.onload=function(){
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
var oBtn=document.getElementById('resBtn');
var oSelect=document.getElementById('opt');
oBtn.onclick=function(){
//數字;
var n1=Number(oTxt1.value);
var n2=Number(oTxt2.value);
if(isNaN(n1)||isNaN(n2)){
alert('請輸入數字類型進行運算');
return
}
//操做符
switch(oSelect.value){
case '+':alert(n1+n2);
break;
case '-':alert(n1-n2);
break;
case '*':alert(n1*n2);
break;
case '/':alert(n1/n2);
break;
default:alert('操做符不正確')
}
}
}
</script>
</head>
<body>
<input type="text" id="txt1">
<select id="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2">
<input type="button" value="計算" id="resBtn">
</body>
</html>
複製代碼