我正在尝试向<base>使用标记的网站添加一些哈希链接(以内容表的格式,以便于网页导航).
现在显然由于基本标记,每个其他相对标记将相对于基本标记href.为了让我创建这个内部内容表,其链接指向特定页面的不同部分,我需要获取默认URL(在基本标记生效之前),以便内部链接可以正常工作.
有没有办法绕过基本标签并实现这一目标?
Juk*_*ela 10
base标记的效果对文档是全局的,并且覆盖效果的唯一方法<base href="...">是使用绝对URL.
如果window.location通过HTTP检索文档,您可以在JavaScript 中使用获取页面本身的URL.您可以使用它来构建绝对URL.
在base通常不需要的标签,这些天.最好使用服务器端技术,使您可以从一个或多个基地址构造地址.所以最好的方法是摆脱标签.
您可以使用一些 javascript 和 jQuery 来“修复”这个问题 -
// get all the internal anchors inside #article
$('#article a').each(
function() {
// get the href attribute
href = $(this).attr('href');
// does it have a href?
if (href) {
// does it have an internal anchor?
if (href.match(/#/i)) {
// append window.location and write back to the href Attribute
new_href = window.location + href;
$(this).attr('href', new_href);
}
}
}
);
Run Code Online (Sandbox Code Playgroud)