当前位置: 代码迷 >> Web前端 >> IE6上onclick跳转失效有关问题
  详细解决方案

IE6上onclick跳转失效有关问题

热度:29   发布时间:2012-11-23 22:54:33.0
IE6下onclick跳转失效问题

<html>
  <head>
    <title>a_test.html</title>
	
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <script type="text/javascript">
    function doClick(){
        self.location.href="MyHtml.html";       
    }
    </script>
  </head>
  
  <body>
    <a href="javascript:void(0);" onclick="doClick();">请点击我</a>
  </body>
</html>

?
这段代码在IE8下没有问题,可以正确跳转到MyHtml.html页面,但是在IE6下面失效了。

?

网上查了很久,没有找到微软的权威说明,但是这个类似:http://support.microsoft.com/kb/190244/en-us

?

问题发现了,解决方法有两种:

1. 推荐做法:在onclick执行过程中加return;

<html>
  <head>
    <title>a_test.html</title>
	
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <script type="text/javascript">
    function doClick(){
        self.location.href="MyHtml.html";       
    }
    </script>
  </head>
  
  <body>
    <a href="javascript:void(0);" onclick="doClick();return false;">请点击我</a>
  </body>
</html>
?

2. 使用‘#’替代'javascript:void(0);',但是‘#’会让页面跳转到top,如果页面有较长会导致用户体验差。

<html>
  <head>
    <title>a_test.html</title>
	
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <script type="text/javascript">
    function doClick(){
        self.location.href="MyHtml.html";       
    }
    </script>
  </head>
  
  <body>
    <a href="#" onclick="doClick();">请点击我</a>
  </body>
</html>
?
  相关解决方案