锚标记在第二级页面中不起作用

问题描述 投票:0回答:2

我有一个包含此内容的菜单:

<div class="menu">
    <ol>
        <li data-target="home" ><a href="#home">hello</a></li>
        <li data-target="contact" ><a href="#contact">Contact</a></li>
        <li data-target="policy_privacy" ><a href="https://somesite.com/privacy_policy">Privacy</a></li>
    </ol>
</div>

当我在home页面上时,锚点运行良好(#home#contact),但是如果我在privacy_policy页面上并尝试转到主页,则不会发生任何事情。如何以某种方式检测我是否不在主页(包含锚点)中,并将用户重定向到主页?

html anchor anchor-scroll
2个回答
0
投票
尝试

<div class="menu"> <ol> <li data-target="home" ><a href="#home">hello</a></li> <li data-target="contact" ><a href="#contact">Contact</a></li> <li data-target="policy_privacy" ><a href="https://www.google.com/">Privacy</a></li> </ol> </div>

0
投票
如果要为privacy_policy页面添加单独的标记(您可以在其中添加锚标记中的首页链接似乎不容易,那么您应该尝试这样的事情。

if (window.location.href.includes("privacy_policy")) { document.querySelector('#home').href = "https://somesite.com"; }

© www.soinside.com 2019 - 2024. All rights reserved.