0

使用 jquery 地址,我正在设置我的 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") {
        ...
    }
});
4

2 回答 2

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>
于 2011-09-07T17:50:56.503 回答
0

尝试使用 $(this).prop("href") 来获取 url。它应该在所有浏览器中为您提供相同的值。

编辑:路径名在 IE 中不起作用。

..猜我应该做更多的测试。路径名在 IE 中不起作用。只需使用“href”

于 2011-09-02T15:49:33.720 回答