js IE6超級連結window.location.href不跳轉的bug解決辦法

來源:互聯網
上載者:User

今天遇到個很詭異的問題,就是<a href=”javascript:void(0);” onclick=”window.location.href=url”></a>在IE6下面沒反應,不跳轉到onclik事件中的“window.location.href”。

當時我們在網上找了篇文章很快就解決了,但是文章中沒有說明具體原因在哪裡,只是說在“window.location.href”後面加一個”return false“,當時馬上建了個test.html,試了下確實可以,而且試了之後還發現IE6下是被href=”javascript:void(0)”覆蓋了,這問題看上去很簡單,但是為什麼其他瀏覽器沒有被覆蓋,但對原因到底在哪裡還是一頭霧水。

IE6 頁面跳轉事件,必須返回false 或者 阻止預設事件,才能進行正常的頁面跳轉

實驗如下:

 代碼如下 複製代碼

var goUrl = function(url) {
 if (!url) return ;
 window.location.href = url;
}


1、<a href=”javascript:;” onclick=”javascript:goUrl(‘http://www.baidu.com’);”>跳轉1</a>

2、<a href=”javascript:void(0);” onclick=”javascript:goUrl(‘http://www.baidu.com’);”>跳轉2</a>

3、<a href=”javascript:void(0);” onclick=”javascript:goUrl(‘http://www.baidu.com’);return false;”>跳轉3</a>

4、<a href=”#” onclick=”javascript:goUrl(‘http://www.baidu.com’);”>跳轉4</a>

5、<a href=”###” onclick=”javascript:goUrl(‘http://www.baidu.com’);”>跳轉5</a>

 

其中,3、4、5 在ie6下面都可以跳轉。1,2不能跳轉。

原因:使用return false 或者 架構(Ext jQuery)內建的禁用事件方法 來屏蔽預設事件。

注意:在jQuery事件中,要用 event.preventDefault(); 來阻止預設事件的:

 代碼如下 複製代碼

<a href="javascrpt:;" id="recommendGoodsAdd">添加推薦產品</a>
//
$("#recommendGoodsAdd").click(function(event){
 window.location.href = "http://www.111cn.net/";
 event.preventDefault();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.