Math.round(3.6) //四舍五入
random() //返回0-1之間的隨機(jī)數(shù)
max(num1, num2) //返回較大的數(shù)
min(num1, num2) //返回較小的數(shù)
abs(num) //絕對(duì)值
ceil(19.3) //12 向上取整
floor(11.8) //11 向下取整
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" />
<title>無(wú)標(biāo)題文檔</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="獲取驗(yàn)證碼" id="btn" />
<input type="text" value="5345" id="txt" />
</body>
</html>
2、驗(yàn)證
<!DOCTYPE HTML>
<html>
<head>
<title>注冊(cè)頁(yè)面</title>
<meta charset="utf-8" />
<script type="text/javascript">
//可以對(duì)輸入的文本做簡(jiǎn)單校驗(yàn)
function btnClick() {
var oUsername = document.getElementById("box_username");
var oPassname = document.getElementById("box_password");
var oSpan = document.getElementById("box_span");
//清楚掉兩個(gè)文本中的空格
var sUsername = oUsername.value.replace(/ /g, "");
var sPassname = oPassname.value.replace(/ /g, "");
//取出用戶名第一個(gè)字符
var unChar = sUsername.charAt(0);
if (unChar >= '0' && unChar <= '9') {
oSpan.innerHTML = "用戶名首字母不可以是數(shù)字!";
} else if (sUsername.length == 0) {
oSpan.innerHTML = "用戶名不能為空!";
} else if (sUsername.length < 6 || sUsername.length > 20) {
oSpan.innerHTML = "用戶名長(zhǎng)度應(yīng)該不小于6并且不大于20!";
} else if (sPassname.length == 0) {
oSpan.innerHTML = "密碼不能為空!";
}
//將處理完空格的文本替換掉原來(lái)錯(cuò)誤的文本
//替換文本框中的值
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.獲取需要驗(yàn)證的字符串;
4.對(duì)字符串進(jìn)行處理(1.用戶名不能為空 。2.開頭字母不能是數(shù)字。3.用戶名長(zhǎng)度應(yīng)該小于6位但是不能大于20位。4.密碼不能為空。);
5.驗(yàn)證字符串,并將結(jié)果寫入提示區(qū)域內(nèi);
注:空格也是字符;
3.計(jì)算器, 計(jì)算器思路。
布局。(略)
switch()語(yǔ)句和if類似,但是比if更復(fù)雜;
switch(sth){
case sth1:
代碼塊;
break;
default:
代碼塊;
}
一個(gè)小的計(jì)算器:
<!DOCTYPE html>
<html>
<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(){
//數(shù)字;
var n1=Number(oTxt1.value);
var n2=Number(oTxt2.value);
if(isNaN(n1)||isNaN(n2)){
alert('請(qǐng)輸入數(shù)字類型進(jìn)行運(yùn)算');
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="計(jì)算" id="resBtn">
</body>
</html>
聯(lián)系客服