2016-02-20 76 views
0

在我的HTML <head>的ID,我已經定義了以下<base>基本href至/ - 現在HREF不工作

<base href="/" target="_blank"> 

現在我不能鏈接到標籤與標識在同一頁上,如:

<sup id="ref1" name="ref1"><a href="#fn1" target="_top">1</a></sup> 

<p class="footnote" id="fn1" name="fn1"> 
    <a href="#ref1" title="back" target="_top">&#8617;</a> 
</p> 

指向ID的鏈接將我帶回根目錄,而不是指定的ID。我可以以某種方式強制<a>元素在當前文檔中查找ID,而不必刪除<base>中的href="/"

+0

可能的複製[請錨鏈接指當前頁面時使用](http://stackoverflow.com/questions/8108836/make-anchor-links-refer-to-the-current-page-when-using-base) – arcyqwerty

回答

0

嘗試:

$(document).ready(function() { 
    var pathname = window.location.href.split('#')[0]; 
    $('a[href^="#"]').each(function() { 
     var $this = $(this), 
      link = $this.attr('href'); 
     $this.attr('href', pathname + link); 
    }); 
}); 

這將解決所有鏈接或在單獨的鏈接(不jQuery的):

<a href="javascript:;" onclick="document.location.hash='anchor';">Anchor</a> 

來源:Make anchor links refer to the current page when using <base>

+0

謝謝,thi的作品。我還發現了一個解決方案,其中有以下工作:'' –

+0

是的,這也適用,假設你在'path/filename.html'。所提供的答案更一般,因爲它允許您使用不依賴頁面名稱的相對定位符。例如,如果您重命名'filename.html' - >'foo.html',則必須更改所有鏈接。這也適用於不同的頁面(每頁相同的代碼)。雖然你的解決方案不使用JS,所以這也是可取的。 – arcyqwerty