Javascript 如何使用内部页面中的锚重定向?
我有一个带有锚和内页的登录页。当我点击内部页面中的锚时,理想情况下我需要内部页面转到主页并滚动到锚。或者至少重定向到主页。 我试试这个:Javascript 如何使用内部页面中的锚重定向?,javascript,Javascript,我有一个带有锚和内页的登录页。当我点击内部页面中的锚时,理想情况下我需要内部页面转到主页并滚动到锚。或者至少重定向到主页。 我试试这个: function linksMenu() { let anchors = document.querySelectorAll('a[href*="#"]'); for (let anchor of anchors) { anchor.addEventListener('click', function(e) { e.prevent
function linksMenu() {
let anchors = document.querySelectorAll('a[href*="#"]');
for (let anchor of anchors) {
anchor.addEventListener('click', function(e) {
e.preventDefault()
if (window.location.path != '/' && window.location.path != '#') {
location.href = '/'
}
let blockID = anchor.getAttribute('href');
document.querySelector(blockID).scrollIntoView({
behavior: 'smooth',
block: 'start'
})
})
}
}
linksMenu();
但脚本首先尝试滚动,然后重新加载,无论我们在哪里。编辑:
此外,如果指向您的登录页的链接是 “mywebsite.com/index”,您要滚动到, 那么重定向链接应该是mywebsite.com/index#two
//假设你是https://example.com/?section=one
var callback=function(){
if(urlParams.has('section')){
const section=urlParams.get('section');
var元素=document.getElementById(节);
if(typeof(element)!='undefined'&&element!=null){
document.getElementById('id').scrollIntoView({
行为:“平滑”
});
}
}
};
如果(
document.readyState==“完成”||
(document.readyState!=“加载”&&!document.documentElement.doScroll)
) {
回调();
}否则{
addEventListener(“DOMContentLoaded”,回调);
}
你好
这一段有很大的底边空白
因此,页面肯定会皱缩和
将以下链接放在页面折叠下方。
不确定是否了解范围。您是否尝试过链接到#myanchorid
?它代表你想要链接到的锚的id。我在cms上工作。所有页面上的菜单都重复,所以这是一个问题。如果我找不到解决方案,则在菜单中显示:无,并尝试将按钮(重新定位到主页)添加到内部页面类。此外,如果指向您的登录页的链接是例如“mywebsite.com/index”,并且您希望滚动到
,则重定向链接应该是mywebsite.com/index\two
<script>
// assuming ur is https://example.com/?section=one
var callback = function(){
if (urlParams.has('section')) {
const section = urlParams.get('section');
var element = document.getElementById(section);
if (typeof(element) != 'undefined' && element != null) {
document.getElementById('id').scrollIntoView({
behavior: 'smooth'
});
}
}
};
if (
document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
callback();
} else {
document.addEventListener("DOMContentLoaded", callback);
}
</script>