2009-11-16 90 views
0

在頭:單擊錨標籤時可以防止onbeforeunload被調用嗎?

<head> 
    <script type="text/javascript">       
     $(document).ready(function() { 
      $("#anchor_id").click(clickHandler); 
      window.onbeforeunload = confirmExit; 
     }); 

     function clickHandler() { 
      /* hide/show some hidden div's */ 
      $("body").append("<p>Hi there</p>"); 
     } 

     function confirmExit() { 
      return "There are some unsaved changes on page."; 
     } 
    </script> 
</head> 

在車身:

<a id="anchor_id" href="javascript: void(0);">Click Me</a> 

是否有可能阻止onbeforeunload在Internet Explorer這個鏈接,當點擊被稱爲 ?在瀏覽器的其餘 代碼工作正常。

回答

1

嘗試設置href="#"代替,並在clickHandler函數返回false(或防止event.preventDefault()默認事件)

0

試試這個,在FF3.5測試,IE8

<html> 
    <head> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() 
      { 
       $("#anchor_id").click(clickHandler); 
       window.onbeforeunload = function() 
       { 
        return "There are some unsaved changes on page."; 
       }; 
      }); 

      function clickHandler() 
      { 
       /* hide/show some hidden div's */ 
       $("body").append("<p>Hi there</p>"); 
       return false; 
      } 
     </script> 
    </head> 
    <body> 
     <a id="anchor_id" href="javascript: void(0);">Click Me</a> 
    </body> 
</html> 
相關問題