1.在開發過程中我們前端的用戶體驗中有時候會要求點擊一個按鈕,關閉當前瀏覽器窗口。用html DOM就可做到。
2.注意:本次寫法要求在新窗口中關閉。 target="_blank"
3. 由a.html 中打開b.html 在b頁面中點擊按鈕關閉B頁面
4. a頁面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> </head> <body> <div> <a href='button_colse.html' target="_blank">測試點擊按鈕關閉當前頁面</a> </div> </body> </html>
5. b頁面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <style type="text/css"></style> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> </head> <body> <div> 點擊我關閉本頁面 </div> <button onclick="javascript:window.opener=null;window.open('','_self');window.close();" type="button" role="button" aria-disabled="false"> <span class="ui-button-text"> <span>關閉</span> </span> </button> </body> </html>