base href to / - 现在 href to IDs 不起作用

base href to / - now href to IDs not working

在我的 html <head> 中,我定义了以下内容 <base>:

<base href="/" target="_blank">

现在我无法link在同一页面上使用ID标签,例如:

<sup id="ref1" name="ref1"><a href="#fn1" target="_top">1</a></sup>

<p class="footnote" id="fn1" name="fn1">
    <a href="#ref1" title="back" target="_top">&#8617;</a>
</p>

ID 的 links 将我带回根目录而不是指定的 ID。我能以某种方式强制 <a> 元素在当前文档中查找 ID 而不必删除 <base> 中的 href="/" 吗?

尝试:

$(document).ready(function() {
    var pathname = window.location.href.split('#')[0];
    $('a[href^="#"]').each(function() {
        var $this = $(this),
            link = $this.attr('href');
        $this.attr('href', pathname + link);
    });
});

这将修复所有链接或(没有 jQuery)单个链接:

<a href="javascript:;" onclick="document.location.hash='anchor';">Anchor</a>

来源:Make anchor links refer to the current page when using <base>