2016-07-26 78 views
8

下面是我用我的網頁上的代碼在Chrome中無法正常工作,錨<a>標籤使用時#

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

(在其中出現在所有頁面上的菜單)

<a id="Sound"><a> 

(上我想鏈接到的頁面)

我已經嘗試添加內容到帶有ID的標記。但只有在Chrome瀏覽器不會向下滾動到標籤。這些錨點在IE & FF 中有效任何想法?

+0

您是否有鏈接到頁面? – AshboDev

+0

https://dev.phonaudio.com/explore/#Sound –

+0

它需要認證。你可以做一個JSFiddle嗎? – AshboDev

回答

26

原來,這是在特定的Chrome版本中的一個bug,發佈解決辦法的人誰需要它! :)

$(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

感謝隊友,做好找到解決方案。最終到了那裏! – AshboDev

+0

歡呼你的幫助,沒有想到看鉻...... –

+1

沒問題!通常一個淘汰的過程會讓你在那裏! – AshboDev

-2
<html> 
    <body> 
    <li> 
     <a href="#Sound">Sound</a> 
    </li> 
    <a id="Sound" href="www.google.com">I AM CALLED</a> 
    </body> 
</html> 

以此作爲你的代碼就會調用錨標記值id聲音

+0

這就是我已經擁有的,但沒有工作,因爲在鉻特別是一個錯誤,謝謝:) –

3

發佈的解決辦法沒有工作對我來說,這個搜索了幾天之後終於不過的工作就像一個魅力,所以我想這是值得分享:

$(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; 
      } 
     } 
     }); 
    }); 
-1

只需改變呼叫您與外部的鏈接。

<a href="#nlink" class="external"> </a>