通过jQuery Mobile中的jQuery更改href属性

Change href attribute by jQuery in jQuery Mobile

本文关键字:jQuery href 属性 更改 中的 Mobile 通过      更新时间:2023-09-26

我正在通过jQuery Mobile创建一个应用程序。

我想要一个链接,它重定向到一个页面。例如:

<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加载带有Ajax导航的页面时,它不起作用:

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

当每一页都显示时,如何做到这一点?(我应该使用哪个事件?…)

我应该使用jQuery Mobile的pageshow事件。参见本页pageshow部分。

修改版本的jQuery代码以正确工作:

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