Anchor tags not working in chrome when using #

前端 未结 9 2190
一个人的身影
一个人的身影 2020-12-01 15:50

Here is the code I am using on my page,

  • Sound
  • (in a menu which appears on

    相关标签:
    9条回答
    • 2020-12-01 16:21

      Not sure if this helps at all or not, but I realized my anchors in IE were working but not in Firefox or Chrome. I ended up adding ## to my anchors and this solved the issue.

      example: a href="##policy">Purpose and Policy Statement

      instead of :

      a href="#policy">Purpose and Policy Statement

      0 讨论(0)
    • 2020-12-01 16:24

      Turns out this was a bug in certain versions of chrome, posting workaround for anyone who needs it! :)

      $(document).ready(function () {
              var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
              if (window.location.hash && isChrome) {
                  setTimeout(function () {
                      var hash = window.location.hash;
                      window.location.hash = "";
                      window.location.hash = hash;
                  }, 300);
              }
          });
      
      0 讨论(0)
    • 2020-12-01 16:25

      Try using : For menu page

      <li><a href="/explore/##Sound">Sound</a></li>
      

      in a menu which appears on all pages

      <a id="#Sound"><a>
      

      This works well in the all versions of Chrome!
      I have tested it on my browser.

      0 讨论(0)
    • 2020-12-01 16:31
       <html>
         <body>
          <li>
            <a href="#Sound">Sound</a>
          </li>
          <a id="Sound" href="www.google.com">I AM CALLED</a>
         </body>
      </html>
      

      use this as your code it will call the anchor tag with id value sound

      0 讨论(0)
    • 2020-12-01 16:32

      Simply change the call on your link to external.

      <a href="#nlink" class="external">  </a>
      
      0 讨论(0)
    • 2020-12-01 16:35

      The workaround posted didn't work for me, however after days of searching this finally worked like a charm so I figured it was worth sharing:

       $(function() {
             $('a[href*="#"]:not([href="#"])').click(function() {
               if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                 var target = $(this.hash);
                 target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                 if (target.length) {
                   $('html, body').animate({
                     scrollTop: target.offset().top
                   }, 1000);
                   return false;
                 }
               }
             });
           });
      
      0 讨论(0)
    提交回复
    热议问题