轉載地址:http://blog.csdn.net/forezp/article/details/52801166
1.繼續循環continue;
continue的作用是僅僅跳過本次循環,而整個循環體繼續執行。
語句結構:
for(初始條件;判斷條件;循環後條件值更新)
{
if(特殊情況)
{ continue; }
循環代碼
}
2.JavaScript 創建動態頁面。事件是可以被 JavaScript 偵測到的行爲。 網頁中的每個元素都可以產生某些可以觸發 JavaScript 函數或程序的事件。
比如說,當用戶單擊按鈕或者提交表單數據時,就發生一個鼠標單擊(onclick)事件,需要瀏覽器做出處理,返回給用戶一個結果。
3.鼠標單擊事件( onclick )
onclick是鼠標單擊事件,當在網頁上單擊鼠標時,就會發生該事件。同時onclick事件調用的程序塊就會被執行,通常與按鈕一起使用。
<html>
<head>
<script type="text/javascript">
function add2(){
var numa,numb,sum;
numa=6;
numb=8;
sum=numa+numb;
document.write("兩數和爲:"+sum); }
</script>
</head>
<body>
<form>
<input name="button" type="button" value="點擊提交" onclick="add2()" />
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
4.鼠標經過事件(onmouseover)
鼠標經過事件,當鼠標移到一個對象上時,該對象就觸發onmouseover事件,並執行onmouseover事件調用的程序。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠標經過事件 </title>
<script type="text/javascript">
function message(){
confirm("請輸入密碼後,再單擊確定!"); }
</script>
</head>
<body>
<form>
密碼:<input name="password" type="password" >
<input name="確定" type="button" value="確定" onmouseover="message()"/>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
5.
鼠標移開事件(onmouseout)
鼠標移開事件,當鼠標移開當前對象時,執行onmouseout調用的程序。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠標移開事件 </title>
<script type="text/javascript">
function message(){
alert("不要移開,點擊後進行慕課網!"); }
</script>
</head>
<body>
<form>
<a href="http://www.imooc.com" onmouseout="message()">點擊我</a>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
6.光標聚焦事件onfocus
當網頁中的對象獲得聚點時,執行onfocus調用的程序就會被執行。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 光標聚焦事件 </title>
<script type="text/javascript">
function message(){
alert("請選擇,您現在的職業!");
}
</script>
</head>
<body>
請選擇您的職業:<br>
<form>
<select name="career" onfocus="message">
<option>學生</option>
<option>教師</option>
<option>工程師</option>
<option>演員</option>
<option>會計</option>
</select>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
7.失焦事件(onblur)
onblur事件與onfocus是相對事件,當光標離開當前獲得聚焦對象的時候,觸發onblur事件,同時執行被調用的程序。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 失焦事件 </title>
<script type="text/javascript">
function message(){
alert("請確定已輸入密碼後,在移開!"); }
</script>
</head>
<body>
<form>
用戶:<input name="username" type="text" value="請輸入用戶名!" >
密碼:<input name="password" type="text" value="請輸入密碼!" onblur="message()">
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
8.內容選中事件(onselect)
選中事件,當文本框或者文本域中的文字被選中時,觸發onselect事件,同時調用的程序就會被執行。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 內容選中事件 </title>
<script type="text/javascript">
function message(){
alert("您觸發了選中事件!"); }
</script>
</head>
<body>
<form>
個人簡介:<br>
<textarea name="summary" cols="60" rows="5" onselect="message()">請寫入個人簡介,不少於200字!</textarea>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
9.文本框內容改變事件(onchange)
通過改變文本框的內容來觸發onchange事件,同時執行被調用的程序。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 文本框內容改變事件 </title>
<script type="text/javascript">
function message(){
alert("您改變了文本內容!"); }
</script>
</head>
<body>
<form>
個人簡介:<br>
<textarea name="summary" cols="60" rows="5" onchange="message()">請寫入個人簡介,不少於200字!</textarea>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
10.加載事件(onload)
事件會在頁面加載完成後,立即發生,同時執行被調用的程序。
注意:
a. 加載頁面時,觸發onload事件,事件寫在body標籤內。
b. 此節的加載頁面,可理解爲打開一個新頁面時。
如下代碼,當加載一個新頁面時,彈出對話框“加載中,請稍等…”。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 加載事件 </title>
<script type="text/javascript">
function message(){
alert("加載中,請稍等…"); }
</script>
</head>
<body onload="message()">
歡迎學習JavaScript。
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
11.卸載事件(onunload)
當用戶退出頁面時(頁面關閉、頁面刷新等),觸發onUnload事件,同時執行被調用的程序。
注意:不同瀏覽器對onunload事件支持不同。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 卸載事件 </title>
<script type="text/javascript">
window.onunload = onunload_message;
function onunload_message(){
alert("您確定離開該網頁嗎?");
}
</script>
</head>
<body>
歡迎學習JavaScript。
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
12.任務
使用JS完成一個簡單的計算器功能。實現2個輸入框中輸入整數後,點擊第三個輸入框能給出2個整數的加減乘除。
提示:獲取元素的值設置和獲取方法爲:例:賦值:document.getElementById(“id”).value = 1; 取值:var = document.getElementById(“id”).value;
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
//獲取第一個輸入框的值
var a=document.getElementById("txt1").value;
//獲取第二個輸入框的值
var b=document.getElementById("txt2").value;
//獲取選擇框的值
var c=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
switch(c){
case"+":
var sum=parseInt(a)+parseInt(b);
break;
case"-":
var sum=parseInt(a)-parseInt(b);
break;
case"*":
var sum=parseInt(a)*parseInt(b);
break;
case"/":
var sum=parseInt(a)/parseInt(b);
break;
}
//設置結果輸入框的值
document.getElementById("fruit").value=sum;
}
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick="count()"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->
<input type='text' id='fruit' />
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47