<html><head><title>JS父子視窗</title><script>function alterImg(){var _uname=document.frm.uname.value;//擷取表單中控制項的值var res =showModalDialog("JS強制回應對話方塊.html",_uname,"dialogwidth:300;dialogheight:300;");/*JS強制回應對話方塊.html--強制回應對話方塊開啟的頁面_uname--傳遞給子表單xxx.html的參數,它是一個字串類型propert:對話方塊的屬性設定,如對話方塊寬度,滾動等res--強制回應對話方塊的傳回值,即子表單傳遞給父表單的資料*/if(res!=null) {document.getElementById("image").src=res; //變數不能加引號,注意哪些加引號,哪些不加引號}else {document.getElementById("image").src="head/b1.bmp";}}</script></head><body><center><form name="frm"><table cellspacing="1px" width="400px" border="1px"><caption>表格標題</caption><tr><th colspan="2">使用者註冊</th></tr> <tr><td>使用者名稱</td><td><input type="text" value="" name="uname"></td></tr><tr><td>頭像</td><td><img id="image" src="head/b1.bmp"> <a href="javaScript:alterImg();">換一張</a></td></tr></table></form></center></body></html>
<html><head><title>JS強制回應對話方塊</title></head><script>function init() {//擷取使用者名稱文字框的值//在對話方塊頁面中window.dialogArguments屬性工作表示父表單傳遞過來的參數document.getElementById("sp").innerText=window.dialogArguments;//給span層添加普通文本}function sel(a) {//a指圖片對象a.border="2px";}function unsel(a) {a.border="0px";}function ok(a) {//window.returnValue屬性工作表示子表單準備返回給父表單的傳回值,在子表單關閉後返回window.returnValue=a.src;//window可以去掉window.close();}</script><body onload="init();"> <center> <span id="sp"></span>頭像 <table> <tr> <td> <script> for(var i=1;i<21;i++) { document.write("<img src=head/b"+i+".bmp onmouseover=se1(this) onmouseout=unse1(this) onclick=ok(this)>"); if(i%5==0) { document.write("<br>"); } } </script> </td> </tr> </table> </center></body></html>