2012-08-04 59 views
0

我正在通過jQuery Mobile創建應用程序。在jQuery Mobile中通過jQuery更改href屬性

我想要一個鏈接,它會重定向到一個頁面。例如:

<a href="/Account/" data-transition="turn" class="useroptions">Account</a> 

它是適用於所有的網頁,我想改變每一個頁面上的鏈接的href到這樣的事情:

<a href="/Account/?returnUrl=http%3A%2F%2Fexample.com%2FAbout" data-transition="turn" class="useroptions">Account</a> 

我寫了這個代碼,但它不工作的時候jQuery Mobile的加載與阿賈克斯導航一頁:

$(function() { 
    $(".useroptions").attr("href", "/Account/?returnUrl=" + encodeURIComponent(document.URL)); 
}); 

如何做到這一點時,每一頁都笑WN? (我應該使用哪個事件?...)

+0

你可能有聽'pageinit'事件,根據文檔:http://jquerymobile.com/demos/1.1.1/ docs/pages/page-scripting.html – 2012-08-04 12:22:54

+0

我在某些頁面使用* prefetching *,所以這個事件是不可用的。我需要一個事件,當頁面實際**顯示**時運行... – 2012-08-04 12:23:59

+0

也許'pageshow'可以解決我的問題。看到這個:http://jquerymobile.com/test/docs/api/events.html但我不知道如何應用**所有我的網頁**的事件。不是一個一個...... – 2012-08-04 12:25:14

回答

1

我應該使用jQuery Mobile的pageshow事件。請參閱pageshow部分this頁面。

修改的jQuery代碼版本才能正常工作:

$("div[data-role='page']").live("pageshow",function() { 
    $(".useroptions").attr("href", "/Account/?returnUrl=" + encodeURIComponent(document.URL)); 
});