2011-09-02 33 views
0

使用jQuery地址我設置我的HREF鏈接,像這樣:無法設置與jQuery的地址在IE中正確HREF鏈接

<a href="#view_profile=123">view 123</a> 

當調用我的地址更改事件:

$.address.change(function(e) { 
    alert(e.value); 
}); 

我見FF和Chrome的值如我所料:

/view_profile=123 

但是,IE會返回帶有前面的「/」的完整URL路徑,如下所示:

/http://localhost/#view_profile=123 

任何想法爲什麼IE做到這一點,什麼是解決它的最好方法?我嘗試了幾件事,但每次都是一樣的。

下面是我用得到的鏈接路徑代碼:

// Setup jQuery address on some elements 
$('a').address(); 

// Run some code on initial load 
$.address.init(function(e) { 
    // Address details can be found in the event object 
}); 

// Handle any URL change events 
$.address.change(function(e) { 
    alert(e.value); 

    var urlAux = e.value.split('='); 
    var page = urlAux[0]; 
    var arg = urlAux[1]; 

    alert("page: " + page); 
    alert("arg: " + arg); 

    if (page == "/view_profile") { 
     ... 
    } 
}); 
+0

什麼是'e'以及它是如何與你的錨? – Phil

+0

e是我的事件對象,並保存鏈接的值,在這種情況下, – Paul

+1

解決方案將基於如何獲取鏈接值。你可以發佈該代碼嗎?發生這種情況的原因是,當您在IE中動態創建錨點標記時,無論您是否只爲其提供相對路徑,href屬性將始終包含絕對路徑。例如,看到這個jsfiddle。 http://jsfiddle.net/xKLKZ/ –

回答

1

我找到了解決辦法。如果我添加一個類標記到鏈接並在類上尋找單擊事件,我可以設置URL並正確觸發jquery地址更改事件。

$(".testclass").click(function() { 

    var id = null; 
    if ($.browser.msie) {  
     id = $(this).attr('href').slice($(this).attr('href').indexOf('#')+1); 
    } 
    else {  
     id = $(this).attr('href').substr(1); 
    } 

    alert("ID: " + id); 

    location.href = "#view_profile=" + id; 
    return false; 
}); 

我也不得不href值設置爲ID如下:

<a href="#123" class="testclass">123</a> 
0

嘗試使用$(本).prop( 「HREF」)來獲取URL。它應該在所有瀏覽器中獲得相同的值。

編輯:路徑名在IE中不起作用。

..猜我應該做更多的測試。路徑名在IE中不起作用。只是使用「href」