JavaScript 是客戶端的 Script 語言,可以在某些事件觸發之後,改變目前網頁的狀態或屬性,達到"動態"網頁。另外也可以用在使用者輸入資料驗證 ... 等,客戶端的應用。
需求:
1. 父視窗點選按鈕開啟子視窗。
2. 視窗開啟後,按鈕從"開啟"變成"關閉"。
3. 子視窗選擇完之後,可以把值傳回父視窗。
4. 點選父視窗的"關閉"按鈕,可以關閉子視窗。
5. 若直接點選子視窗右上角的 X 關閉,父視窗的"關閉"按鈕要顯示"開啟"。
6. 當父視窗關閉時,子視窗一同關閉。
範例連結<html> <head> <title>父視窗</title> </head> <body onunload="fclose()"> <input type="button" onClick="show_sign('NAME')" name="sign2" id="sign" value="開啟"/> <INPUT TYPE="text" NAME="NAME" id="NAME"> <script> var newwin = null; function show_sign(wh) { if (newwin == null) { document.getElementById("sign").value = '關閉'; newwin = window.open("1.htm", "子視 窗", "toolbar=no,location=no,directories=no,directorybuttons=no, status=no,menubar= no,scrollbars=no,resizable=no,width=455,height=120"); } else { document.getElementById("sign").value = '開啟'; newwin.close(); newwin = null; } } function fclose() { if(newwin != null) newwin.close(); } </script> </body> </html> |
<html> <head> <title>子視窗</title> </head> <body onunload="close1()"> <a onclick="bt('你')"><font style="cursor:hand">你</font></a> <a onclick="bt('好')"><font style="cursor:hand">好</font></a> <a onclick="bt('嗎')"><font style="cursor:hand">嗎</font></a> <script> function bt(str) { var sTextValue = str; window.opener.document.getElementById("NAME").value += sTextValue; } function close1() { window.opener.newwin = null; window.opener.document.getElementById("sign").value = '開啟'; } </script> |