简体   繁体   English

需要在 html 锚点上单击两次到 go 到 id 或名称

[英]need clicked twice on html anchor to go to id or name

I have this little problem happen on firefox (works fine on chrome), i have list of buttons that have anchor with different in page destination, im using id, but (on firefox) the anchor only work on second click, the first click only change the URL.我在 firefox 上发生了这个小问题(在 chrome 上工作正常),我有一个按钮列表,这些按钮在页面目标中具有不同的锚点,我使用 id,但是(在 Firefox 上)锚点只在第二次点击时有效,第一次点击只更改 URL。

<a href="#section1">button 1</a>
<a href="#section2">button 2</a>

destination目的地

<section id="section1">
some content
</section>
<section id="section2">
some content
</section>

ive also already try name="" but its still need double clikc to work, is there method to resolve this or is it a bug on firefox?我也已经尝试过 name="" 但它仍然需要双击才能工作,是否有解决此问题的方法或者它是 firefox 上的错误?

Try this:尝试这个:

<a href="javascript:document.getElementById('section1').scrollIntoView(true);">
    Button 1
</a>
<a href="javascript:document.getElementById('section2').scrollIntoView(true);">
    Button 2
</a>

Destination:目的地:

<section id="section1">
    some content
</section>
<section id="section2">
    some content
</section>

I can verify the issue exists in Firefox and Safari.我可以验证 Firefox 和 Safari 中是否存在问题。 This can be solved without JS by configuring your router's scrollBehavior as mentioned in the docs ( Router v3 | Router v4 ):这可以在没有 JS 的情况下通过配置您的路由器的 scrollBehavior 来解决,如文档( Router v3 | Router v4 )中所述:

A solution would look like this for Router V3: Router V3 的解决方案如下所示:

  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return { selector: to.hash };
    } 
  }

Or for Router V4:或者对于路由器 V4:

  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return { el: to.hash };
    }
  }

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

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