简体   繁体   English

使用锚点 div id 链接到另一个页面

[英]Link to another page with anchor div id

So I have a number of dropdown links on my main navigation that need to link to other landing pages and scroll or show specific part of that page, like an article or a specific service.因此,我的主导航上有许多下拉链接,需要链接到其他登录页面并滚动或显示该页面的特定部分,例如文章或特定服务。 I have went through a lot of examples here but can't get this to work:我在这里经历了很多例子,但无法让它发挥作用:

<a class="dropdown-item" href="#" onclick="location.href = '/voice-data#broadband/'">BROADBAND & MANAGED INTERNET</a>

The above link should anchor to this div on another page:上面的链接应该锚定到另一个页面上的这个 div:

<div class="row broadband-block" id="#broadband">

At the moment it lands on the page it needs but doesn't go to the anchor div that it's supposed to,but it does add #broadband to the url in the browser.目前它登陆它需要的页面,但没有进入它应该进入的锚点 div,但它确实将#broadband 添加到浏览器中的 url。 What am I doing wrong and what would be the best solution, as I have quite a few links like that to do?我做错了什么,最好的解决方案是什么,因为我有很多这样的链接要做?

Your problem is the ID attribute.您的问题是 ID 属性。 Remove the # from your ID and it should fix your problem:从您的 ID 中删除 #,它应该可以解决您的问题:

<div class="row broadband-block" id="#broadband">
<div class="row broadband-block" id="broadband">

Or adjust your 'a' tag and add an extra '#' to the front of your url:或者调整您的“a”标签并在您的网址前面添加一个额外的“#”:

<a class="dropdown-item" href="#" onclick="location.href = '/voice-data##broadband/'">BROADBAND & MANAGED INTERNET</a>

Also, javascript onclick not needed unless this is what your application environment requires, it can all be done inside href attribute.此外,除非您的应用程序环境需要,否则不需要 javascript onclick,这都可以在 href 属性中完成。

you should do this你应该做这个

<a class="dropdown-item" href="/voice-data/#broadband">BROADBAND & MANAGED INTERNET</a>

just use the href instead of onclick.只需使用href 而不是onclick。

and the ids should match, at the current state you should be using ##broadband while you should use the id as follows:并且 id 应该匹配,在当前状态下,您应该使用 ##broadband 而您应该使用 id,如下所示:

<div class="row broadband-block" id="broadband">

Edit: @pointy pointed it out (no pun intended)编辑:@pointy 指出(没有双关语意)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM