[課程概要]

  認識event

②  function

 

[event介紹]

  當有事情發生時,可以寫一些反應

  什麼事情?

   點擊某些東西

   移動滑鼠

   按某一個按鍵

 

[onload event]

  擺放位置:放在<body>中,且onload後面要加雙引號

(但不建議這樣做!建議用function來寫)

<html>

<body onload= " alert('Hello!');

                alert('We are going to...');

                prompt(' Excited? ') " >

</body>

</html>

 

[function的基本介紹]

  擺放位置:<script> function </script>

<html>

<head>

<script>

function greeting(){

alert('Hello!');

alert('We are going to...');

prompt(' Excited? ')

}

</script>

</head>

<body onload= " greeting() " >

</body>

</html>

 

[function的用途]

  傳值給fuction

   函式的結構

function purchase(number){

}

   使用此函式

purchase(10);

  從function中得到一個值

   函式的結構

function do_somthing(){

return answer; }

   使用此函式

result = do_something();     // result = answer

  舉例

  用onload呼叫函式

  執行check_user_age時,使用到user_age

  執行user_age,回傳ageint值給user_age函式,進而跟18比大小

1.PNG

 

  function也可以呼叫自己本身

   函式的結構

function do_somthing(control_value){

return answer; }

   使用此函式

result = do_something();

   舉例

  • 想要跳出一個視窗顯示5個great
  • 執行great函式,如果回傳的值(depth)大於0,就會回傳一個great,並且再次執行一次great,而此時的depth = depth -1

2.PNG

 

arrow
arrow
    文章標籤
    javascript event coursera
    全站熱搜
    創作者介紹
    創作者 atina 的頭像
    atina

    網頁設計的學習筆記

    atina 發表在 痞客邦 留言(0) 人氣()