javaScript的單元測試題

把自己做的一些測試題整理了發出來,如果對你有用,可以點贊關注,共同學習。

第一、二章理論知識測試題

第二章實操

第三章測試題

第四章測試題

1、下列( )表達式產生一個0~7之間(含0,7)的隨機整數。
A、Math.floor(Math.random()*6)
B、Math.floor(Math.random()*7)
C、Math. floor(Math.random()*8)
D、Math.ceil(Math.random()*8)
答案:C
2、將Array對象中的元素值進行輸出的方法是( )。
A、用下標獲取指定元素值
B、用for語句獲取數組中的元素值
C、用數組對象名輸出所有元素值
D、以上3種方法都可以
答案:D
3、下面JavaScript語句中能正確輸出“H2O”的字符串表達式是( )。
A、str=“2”; document.write(“H”+str.sub()+“O”);
B、str=“2”; document.write(“H”+str.sup()+“O”);
C、str=“2”; document.write(H+str.sub()+O);
D、str=“2”; document.write(H+str.sup()+O);
答案:A
4、下列關於Date對象的getMonth()方法的返回值描述,正確的是( )。
A、返回系統時間的當前月
B、返回值的範圍介於1~12之間
C、返回系統時間的當前月+1
D、返回值的範圍介於0~11之間
答案:D
5、下面的4個方法中,不是String對象的方法的是( )。
A、charAt()
B、substring()
C、toUpperCase()
D、length()
答案:D
6、對字符串str=“welcome to china"進行下列操作處理,描述結果正確的是( )。
A、str.substring(1,5)返回值是"elcom”
B、str.length的返回值是16
C、str.indexOf(“come”,4)的返回值爲4
D、str.toUpperCase()的返回值是"Welcome To China"
答案:B
7、分析下面的JavaScript代碼段,輸出結果是( )。
var mystring=“I am a student”;
var a=mystring.substring(9,13);
document.write(a);
A、stud
B、tuden
C、uden
D、udent
答案:C
8、若 var num = 10.5; 則 num.toFixed(2) 的值爲( )。
A、10.50
B、10.5
C、10
D、10.500
答案:A
9、若字符串的indexOf()方法查找失敗,則返回( )。
A、0
B、-1
C、false
D、null
答案:B
10、獲取當前的星期值,使用Date對象的( )方法。
A、getDate()
B、getDay()
C、getTime()
D、getWeek()
答案:B
11、在Math對象中,獲取絕對值的方法爲( )。
A、sqrt()
B、floor()
C、pow()
D、abs()
答案:D
12、爲Date對象設置年份使用( )方法。
A、getFullYear()
B、setFullYear()
C、getDate()
D、setDate()
答案:B
13、Math.round(-4.60)的計算結果爲( )。
A、-4.6
B、-4.0
C、-5.0
D、-5
答案:D
14、若 var str = ‘abc’; 則 str[1] 的值爲。( )
A、a
B、b
C、c
D、語法錯誤,不能獲取其值
答案:B
15、獲取一個字符在字符串中首次出現的位置,使用( )方法。
A、charAt()
B、indexOf()
C、lastIndexOf()
D、substr()
答案:B
16、數組的索引值是從__________開始的。
答案:0
17、通過構造函數創建實例對象的關鍵字是 。
答案:new
18、獲取字符串變量str的長度的代碼爲 。
答案: str.length
19、若要從變量str保存的字符串中截取從位置5開始的後面2個字符,使用 。
答案:str.substr(5,2)
20、在JavaScript中,獲取基數的指數次冪,使用Math對象的 方法。
答案:pow()

第四章實驗操作測試題

1、【簡答題】
判斷用戶名和密碼文本框不能爲空,同時要求密碼在4到8位之間。
在這裏插入圖片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>判斷用戶名和密碼</title>
	</head>
	<body>
			
		<h2>請輸入4位到8位之間的密碼!!!!!</h2>
		<hr /><br />
		<form name="form">
		姓名:<input type="text" name="name" /><br />
		密碼:<input type="password" name="password" /><br /><br />
          <input type="button" value="確認" onclick="test()" />
		  </form>
		<script type="text/javascript">
			function test(){
				var n=document.form.name.value.length;
				var p=document.form.password.value.length;
				if(n==0){
					alert("姓名不能爲空!")
				}
				else{
				if(p==0){
					alert("密碼不能爲空");
				}
				else if(!(p>=4&&p<=8)){
					alert("請輸入4位到8位之間的密碼!!!!!")
				}
				else
				alert("輸入正確")
			}
			}
		</script>
	</body>
</html>

在這裏插入圖片描述
在這裏插入圖片描述
2、【簡答題】
網頁的文本框中顯示時鐘,顯示格式如下:
在這裏插入圖片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>網頁的文本框中顯示時鐘</title>
	</head>
	<body>
		電子時鐘:<br />
		<input type="text" value="" id="time" />
		<script type="text/javascript">
			var now=new Date();
			var hour=now.getHours();
			var minute=now.getMinutes();
			var second=now.getSeconds();
			if(hour<10){
				hour="0"+hour;
			} 
			if(minute<10){
				minute="0"+minute;
			}
			if(second<10){
				second="0"+second;
			}
			document.getElementById("time").value="現在的時間是:"+hour+":"+minute+":"+second;
		</script>
	</body>
</html>

在這裏插入圖片描述

第五章理論知識測試

1、在鍵盤上的某個鍵被按下並且釋放時觸發的事件是( )事件。
A、onkeypress
B、onkeydown
C、onkeyup
D、onclick
答案:A
2、下面( )不是鼠標鍵盤事件。
A、onclick事件
B、onmouseover事件
C、oncut事件
D、onkeydown事件
答案:C
3、下面( )不屬於頁面事件。
A、onload事件
B、onunload事件
C、onkeyup事件
D、onresize事件
答案:C
4、當鼠標指針移到頁面上的某個圖片上時,圖片出現一個邊框,並且圖片放大,這是因爲觸發了下面的( )事件。
A、onclick
B、onmousemove
C、onmouseout
D、onmousedown
答案:B
5、在HTML頁面中,不能與onChange事件處理程序相關聯的表單元素有( ) 。
A、文本框
B、複選框
C、列表框
D、按鈕
答案:D
6、在HTML頁面中包含一個按鈕控件mybutton,如果要實現單擊該按鈕時調用自定義的JavaScript函數compute(),要編寫的HTML代碼是( )。
A、< input name=“mybutton” type=“button” onBlur=“compute()” value=“計算”>
B、< input name=“mybutton” type=“button” onFocus=“compute()” value=“計算”>
C、< input name=“mybutton” type=“button” onClick=“function compute()” value=“計算”>
D、< input name=“mybutton” type=“button” onClick=“compute()” value=“計算”>
答案:D
7、在HTML文檔中包含如下超鏈接,要實現當鼠標移入該鏈接時,超鏈接文本大小變爲30px,下列選項中編碼正確的是( ) 。
A、< a href="#" οnmοuseοver=“this.style.fontSize=‘30px’”>註冊
B、< a href="#" οnmοuseοut=“this.style.fontsize=30px”>註冊
C、< a href="#" οnmοuseοver=“this.style.font-size=‘30px’”>註冊
D、< a href="#" οnmοuseοut=“this.style.font-size=30px”>註冊
答案:A
8、事件( )可偵測用戶在某元素內連續移動的行爲。
A、mouseover
B、mouseout
C、mouseup
D、mousemove
答案:D
9、在HTML頁面中,當按下鍵盤上的任意一個鍵時都會觸發JavaScript的( )事件。
A、onFocus
B、onBlur
C、onSubmit
D、onKeyDown
答案:D
10、有一個submit按鈕,在這個按鈕控件上添加下列哪個事件不起作用?( )
A、onmouseout
B、onmouseover
C、onclick
D、onsubmit
答案:C
11、獲得焦點的事件是________,失去焦點的事件是________。
答案:
第一空:
onfocus
第二空:
onblur
12、
當前元素失去焦點並且元素的內容發生改變時觸發的事件是___________事件。
答案:
第一空:
onchange
13、當鼠標移動到圖片上時,它會切換爲其他圖片,當鼠標移走時,又恢復爲原來的圖片,這是對JavaScript事件中的___________和___________事件的應用。
答案:
第一空:
onmouseover
第二空:
onmouseout

第五章實驗操作測試題

1、在文本框分別輸入兩個數,實現兩個數的+、-、*、/運算,點擊“=按鈕將”結果顯示在文本框中。界面設計可參考下圖。注意對文本框輸入的數據是否是數字要進行判斷。
在這裏插入圖片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>四則運算</title>
	</head>
	<body>
		<p>請輸入兩個數進行簡單的運算:</p>
		<input type="text" id="num1" />
		<select id="selete">
			<option value="+">+</option>
			<option value="-">-</option>
			<option value="*">*</option>
			<option value="/">/</option>
		</select>
		<input type="text" id="num2" />
		<input type="button" value="=" onclick="test()" />
		<input type="text" id="result" />
		<script type="text/javascript">
			function test() {
				var n1 = parseFloat(document.getElementById("num1").value);
				var n2 = parseFloat(document.getElementById("num2").value);
		
				var s1 = document.getElementById("selete").value;
				switch (s1) {
					case '+':
					 answer= n1 + n2;
						break;
					case '-':
					answer= n1 - n2;
						break;
					case '*':
					answer= n1 * n2;
						break;
					case '/':
					answer= n1 / n2;
						break;
				}
				document.getElementById("result").value=answer;
			}
		</script>
	</body>
</html>

第六章理論知識測試題

1、下面的方法中,( )不是document對象的方法
A、 createElement()
B、getElementById()
C、write()
D、reload()
答案:D
2、獲取頁面中超鏈接的數量的方法是( )。
A、document.links.length
B、document.length
C、document.links[1].length
D、document.links[0].length
答案:A
3、某網頁中有一個表單對象mainForm,該表單對象中的一個元素是文本框username,表述該文本框的方法是( )。
A、document.forms.username
B、document.mainForm.username
C、document.forms.UserName
D、document.MainForm.UserName
答案:B
4、在Javascript中要改變頁面文檔的背景色,需要修改document對象的( )屬性。
A、backColor
B、backgroundColor
C、bgColor
D、background
答案:C
5、下列對錶單對象的屬性表述不正確的是( )
A、name:返回表單的名稱
B、action:返回/設定表單的提交地址
C、target:返回/設定表單提交內容的編碼方式
D、length:返回該表單所含元素的個數
答案:C
6、分析下面這段代碼運行的結果( )。
< script type=“text/javascript”>
with(document){
writeln(“最後一次修改時間:”+document.lastModified+"
");
writeln(“標題:” +document.title+"
");
writeln(“URL:” +document.URL+"
");
}
< /script>
A、只輸出最後一次修改的時間
B、只輸出文檔的標題
C、最後一次修改時間、輸出文檔的標題和當前的URL
D、什麼也不輸出
答案:C
7、向文檔中動態添加一個HTML標記可以使用____________方法實現。
答案:
第一空:
createElement()
8、表示文檔中的未訪問過的超鏈接顏色的屬性是 _屬性。
答案:
第一空:
linkColor
9、form對象只有兩個方法,分別是___________和
,這兩個方法類似於單擊了重置按鈕和提交按鈕。
答案:
第一空:
reset()
第二空:
submit()

熱愛開源,喜歡探索未知,知道的越多,才發現不知道的越多,點贊關注,一起學習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章