(利用DOM)在新打開的頁面點擊關閉當前瀏覽器窗口


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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM