简体   繁体   English

锚标记在 Google Chrome 中不起作用(链接到单独的网页时)

[英]Anchor tags not working in Google Chrome (when linking to separate webpage)

I'm a bit new to html, javscript, and css so I apologize ahead of time for any obvious ignorance.我对 html、javscript 和 css 有点陌生,所以对于任何明显的无知,我提前道歉。 I'm using an anchor tag so that when a user clicks on this link it will jump them to a specific div on another webpage:我正在使用锚标记,以便当用户单击此链接时,它将跳转到另一个网页上的特定 div:

index.html :索引.html

<div class="notice">
  <h2>If you have any trouble while you're here, please <br>
  <a href="/contact.html#contactForm"> get in touch with me</a> and let me 
        know!</h2>
</div>

contact.html :联系方式。html

<div class=contact id="contactForm">

However, when you click on the link, Chrome ignores the # and just loads /contact.html.但是,当您单击链接时,Chrome 会忽略 # 并仅加载 /contact.html。 But then, when you navigate back to /index.html and then click on the link again - then Chrome decides to pay attention to the # and loads /contact.html#contactForm.但是,当您导航回 /index.html 并再次单击该链接时,Chrome 决定关注 # 并加载 /contact.html#contactForm。

I've researched javascript threads that use various scroll() functions - but they all seem to work when linking to a place on the same webpage.我研究了使用各种 scroll() 函数的 javascript 线程 - 但它们在链接到同一网页上的某个位置时似乎都有效。 I'm having trouble finding a workaround for when the anchor is on a different page.当锚点位于不同页面时,我无法找到解决方法。

UPDATE I've also noticed that it seems to be the fault of random Chrome plugins - as with all plugins disabled, the anchor tag works just fine.更新我还注意到这似乎是随机 Chrome 插件的错误 - 与所有插件禁用一样,锚标签工作得很好。 However, I can't exactly expect all visitors to the website to have all their plugins disabled.但是,我不能完全期望网站的所有访问者都禁用所有插件。

You actually need to add an anchor element with a name attribute containing the hash string, this element may be empty.您实际上需要添加一个name属性包含 hash 字符串的锚元素,该元素可能为空。 Use this:用这个:

<a name="contactForm"></a>
<div class=contact id="contactForm">
...your code

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

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