1

所以我下面的代码适用于所有浏览器,但是在 IE8 和 IE7 中,链接似乎没有保留文本。

正在发生的事情的示例:

<a href="http//site.com/">view site</a>

在我在 IE 中的代码之后

<a href="http//site.com/">http//site.com/</a>

我做了一些搜索,并且记录了多个问题错误。

我正在使用 jQuery 1.4.2,但也尝试了 1.10 并没有区别。

var replacer = $('.ir a, .tk a');
//apply menu to inner links
$(replacer).each(function () {
    //get page param with getmenu    
    var getmenu = location.href.split("&menu")[1];
    if (typeof getmenu === 'undefined') {
        getmenu = "&menu=1";
    };
    var attr = $(this).attr("href");
    $(this).attr("href", attr + getmenu);
});
4

1 回答 1

1

这似乎仍然是最新的 jQuery (1.11.3) 报告四年后的问题。最简单的解决方案是在使用 jQuery 更改 href 属性后重置文本。

由于 jQuery 更改了文本以及 href,因此只需在更改之前缓存文本并将文本设置回原来的样子:

// Keep a copy of the text before jQuery messes it up.
var text = $(this).text();
// Change the href.
var attr = $(this).attr("href");
$(this).attr("href", attr + getmenu);
// Set the text back to what it was.
$(this).text(text);

这对于所有浏览器版本都非常安全,因此您无需在应用此解决方法之前检查它是否为 IE<9。

于 2015-06-28T09:10:52.390 回答