在我的网站页脚中,有链接可将用户重定向到网站的公司页面,例如www.example.com/company.html#first。
点击此链接会触发默认的浏览器行为,即它会重定向到www.example.com/company.html#first并显示id ="第一个"的div。
但是在我的company.html的dom中,没有id ="第一个" (我有我的理由,因为我无法向相关的div提供id,但这是一个单独的问题)。此外,我在某些功能中使用了 url&#t>哈希,因此我必须将其保留为 #first 。
所以,我的问题是如果我可以将不同的div带入视图 - div的父元素(具有 id 可用)我未能提供如上所述的id单击页脚链接。建议也欢迎!
答案 0 :(得分:0)
由于您表示您可以访问jQuery,因此您可以编写脚本以在事后更改它们,例如
var link1 = $('a[href="http://www.example.com/company.html#first"]');
$(link1).attr('href','http://www.example.com/company.html#mydiv');
这样你就可以改变你真正希望它指向的网址。你会为每一个人做类似的事情。
答案 1 :(得分:0)
感兴趣的可能是在页脚中提供带有作为data-*
属性显示的后备网址的链接。 E.g。
<a href="www.example.com/company.html#first" data-fallback="www.example.com/company.html#first-parent" title="First">First</a>
在jQuery中你可以做这样的事情(未经测试):
$("a").click(function(e) {
if (window.location.hash) {
// Don't follow the link initially
e.preventDefault();
var url = this.href,
// Borrowed from: http://stackoverflow.com/a/4426201/1150683
el = $(url.substring(url.indexOf('#') + 1));
// If element exists
if (el.length > 0) {
window.location.href = url;
} else {
window.location.href = $(this).data("fallback");
}
}
});
答案 2 :(得分:0)
所以我确实解决了我自己的问题,但只能通过硬编码可用的url哈希。它有点脏,每个页面都会调用脚本,但我认为这就是我现在要用的东西。我像这样使用$(document).ready():
$(document).ready(function() {
if (location.pathname.split('/')[1] === 'company.html' && $.inArray(location.hash.split('#')[1], ['first', 'second', 'third']) > -1) {
$('#parent').get(0).scrollIntoView();
}
});