【问题标题】:Unable to set correct href link in IE with jquery address无法在 IE 中使用 jquery 地址设置正确的 href 链接
【发布时间】:2011-09-02 15:13:57
【问题描述】:

我使用 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") {
        ...
    }
});

【问题讨论】:

  • 什么是e,它与您的主播有什么关系?
  • e 是我的事件对象,在这种情况下保存链接的值
  • 解决方案将基于您获取链接值的方式。你可以发布那个代码吗?发生这种情况的原因是当您在 IE 中动态创建锚标记时,href 属性将始终包含绝对路径,无论您是否只提供了相对路径。例如,请参阅此 jsfiddle。 jsfiddle.net/xKLKZ
  • 请看我上面的代码编辑

标签: jquery internet-explorer jquery-address


【解决方案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>

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多