• 函數:

獨立于主程序的、具有特定功能的一段程序代碼塊。

  • 函數的定義:

格式:
function 函數名([參數[,參數...]]){
     <語句組>
     [return <表達式>;]
}

約定:
1、函數名:易于識別;(同變量命名規則)
2、程序代碼:模塊化設計;
3、函數位置:按邏輯順序,集中置頂。(<head>...</head>)

  • return語句:

格式:
return <表達式>;

功能:返回表達式的值。

  • 函數的調用:

格式:
函數名([參數[,參數...]])

例1:
<Script>

function showName(name){
     document.write ( "我是" + name );
}

showName("黃雅玲");

</Script>

例2:
<Script>

function showName(name){
     str="我是" + name;
     return str;
}

document.write(showName("黃雅玲"));

</Script>

  • 事件:

    用戶對瀏覽器所做的特定的動作(操作),是實現交互操作的一種機制。

事件名稱 適用對象 意義 說明
Abort image 終止 當圖形尚未完全加載前
Blur
失去焦點
Change t/pw/ta/select 改變
DragDrop window 拖曳
Error
img/win 錯誤加載文件或圖形時發生錯誤
Focus
取得焦點
Move window 移動
Reset form 重置
Submit form

Click/DblClick、KeyDown/KeyPress/KeyUp、Load/Unload、MouseDown/MouseUp/MouseOver/MouseOut/MouseMove
  • 事件處理程序:

瀏覽器響應某個事件,實現用戶的交互操作而進行的處理(過程)。

  • 事件處理程序的調用:

    瀏覽器等待用戶的交互操作,并在事件發生時,自動調用事件處理程序(函數),完成事件處理過程。

    HTML標簽屬性:

    格式:
    <tag on事件=“<語句組>|<函數名>”>

    例1:
    <body onload="alert('建議瀏覽器的分辨率:800x600');">
    <body onload="var str='建議瀏覽器的分辨率:800x600';alert(str);">

    例2:
    <script>
    function show(){
         var str="建議瀏覽器的分辨率:800x600";
         alert(str);
    }
    </script>
    <body onload="show();">

    對象屬性:

    格式:
    對象名.on事件=<語句>|<函數名>

    例1:
    document.
    onload=alert("建議瀏覽器的分辨率:800x600");

    var str="建議瀏覽器的分辨率:800x600";
    document.onload=alert(str);

    例2:
    <script>
    function show(){
         var str="建議瀏覽器的分辨率:800x600";
         alert(str);
    }
    document.onload=show();
    </script>

例1:
<Body>
<FORM>
請輸入基本資料:<BR>
姓名:
<INPUT TYPE="text" NAME="usr" SIZE="8">
<INPUT TYPE="button" VALUE=" 請單擊" onClick="alert('謝謝你的填寫...')">
</Body>

例2:
<Script>
function handelError(img){
     msg = "有一圖文件,名為: \'" + img.name + "\'\n無法順利顯示,請通知系統管理人員" + ",敬備薄禮相送。";
     alert(msg);
}
</Script>

<IMG SRC="abc.gif" NAME="中國電信的廣告" onError="handelError(this)">

例3:
<Body>
<A onMouseOver="status='最棒的學習網站';return true;" onMouseOut="status='完畢'">文哥網絡技術學習網</A>
</Body>

例4:
<Body>
<FONT STYLE="cursor:hand" onClick="location='http://www.hubert.idv.tw/'" onMouseOver="status='最棒的在線學習網站'; this.color='red';return true;" onMouseOut="status='完畢'; this.color='blue';">文哥網絡技術學習網</FONT>
</Body>

例5:
<Script>
function mOver(object,msg){
    status = msg;
    object.color = "red";
    object.face = "華文楷體";
}

function mOut(object){
    status = '完畢';
    object.color = "blue";
    object.face = "幼圓";
}
</Script>

<Body>
<FONT STYLE="cursor:hand" onClick="location='http://www.hubert.idv.tw/'" onMouseOver="mOver(this,'最棒的線上學習網站'); return true;" onMouseOut="mOut(this)">文哥網絡技術學習網</FONT>
</Body>

例6:
<STYLE> A {text-decoration:none} </STYLE>

<BODY>
搜尋引擎:<BR>

<IMG SRC="images\snow1.gif" NAME=gif_1>
<A onMouseOver="document.gif_1.src='images\\snow.gif'" onMouseOut="document.gif_1.src='images\\snow1.gif'">蕃薯藤</A>< BR>

<IMG SRC="images\snow1.gif" NAME=gif_2>
<A onMouseOver="document.gif_2.src='images\\snow.gif'" onMouseOut="document.gif_2.src='images\\snow1.gif'">奇摩站</A>
</BODY>

例7:
<Script>

var url = new Array(3);
url[0] = "http://www.yam.org.tw/";
url[1] = "http://www.kimo.com/";
url[2] = "http://chinese.yahoo.com/";

function goto(i) {
     location = url[i];
}

</Script>

<table width=250><tr><td>
<form><fieldset>
<legend>搜尋引擎</legend>
<input name="go" type="radio" onClick="goto(0)">蕃薯藤
<input name="go" type="radio" onClick="goto(1)">奇摩
<input name="go" type="radio" onClick="goto(2)">中文雅虎
</fieldset></form>
</table>

 
  • 定時器:(延遲器)

用以指定在一段特定的時間后執行某段程序。

  • setTimeout():(1.0版)

格式:

[定時器對象名=] setTimeout(“<表達式>”,毫秒)

功能:執行<表達式>一次。



例1:
<Script>

function count() {
     setTimeout("alert('三秒到了')",3000)
}

</Script>

<INPUT TYPE="button" VALUE=" 計時開始" onClick="count()">

例2:
<Script>

function show() {
     document.all['news'].style.display = "";
     setTimeout("hide()",500);
}

function hide() {
     document.all['news'].style.display = "none";
     setTimeout("show()",500);
}

</Script>

<Body onload="show()">
最新消息:<FONT ID="news" STYLE="display:none">十面埋伏...</FONT>
</Body>

  • clearTimeout():終止定時器

格式:

clearTimeout(定時器對象名)

  • setInterval():(1.2版)

格式:

[定時器對象名=] setInterval(“<表達式>”,毫秒)

功能:重復執行<表達式>,直至窗口、框架被關閉或執行clearInterval。

  • clearInterval():終止定時器

格式:

clearInterval(定時器對象名)

例1:
<Script>

var sec = 0;
timerID = setInterval("count()",1000);

function count() {
     num.innerHTML = sec++;
}

</Script>

停留時間:
<FONT ID="num" FACE="impact">0</FONT>秒鐘
<INPUT TYPE="button" VALUE="停止" onClick="clearInterval(timerID)">

例2:
<Script>

var str = "這是一個在線拍賣的網站,請盡情血拼吧!";
var seq = 0;

function scroll() {
     msg = str.substring(0, seq+1);
     banner.innerHTML = msg;
     seq++;
     if (seq >= str.length) seq = 0;
}

</Script>

<Body onLoad="setInterval('scroll()',500)">
<FONT ID="banner"></FONT>
</Body>

 
  • 圖像對象:

網頁中的圖像均會被自動看作圖像對象,并依順序,分別表示為document.images[0],document.images[1]...

  • 建立圖像對象:

格式:

圖像對象名稱=new Image([寬度],[高度])   //px

  • 圖像對象的屬性:

border complete height hspace lowsrc name src vspace width

  • 圖像對象的事件:

onAbort onError onKeyDown onKeyPress onKeyUop onLoad

例1:(預載)
<Script>

img0 = new Image();
img0.src = "images/snow0.gif";

img1 = new Image();
img1.src = "images/snow1.gif";

document.write ("已經讀取兩個圖文件,但此時不顯示。");

</Script>

例2:
<Script>
function img-preload(idx){
     eval("img"+idx+" = new Image()");
     eval("img"+idx+".src = 'images/snow"+idx+".gif'");
}

img-preload(0);
img-preload(1);
document.write ("已經讀取兩個圖文件,但此時不顯示。");

</Script>

例3:
<Script>
function img-preload(imgname,idx){
     eval("img"+idx+" = new Image()");
     eval("img"+idx+".src = 'images/"+imgname+".gif'");
}

img-preload("snow0",0);
img-preload("snow1",1);
document.write ("已經讀取兩個圖文件,但此時不顯示。");

</Script>



ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程