2016-09-30 59 views
1

I've got several anchor tags pointing to internal links mainly to scroll to some section titles or to get to the top from a bottom link of the site since some pages can get very long.內部href的<a tag now refreshes the page

All tags like <a href="#whatever">Whatever</a> just scrolled the page to the <a name="whatever"></a> tag as intended until yesterday, but now when clicked they force a page refresh pointing to the root page with just hash, like this localhost/#whatever instead of this localhost/path/to/current/page/#whatever.

1) Is there even the possibility to alter something in the Apache server, browser settings (not touched tough), HTML/JavaScript/CSS code of a page or whatever to force page refreshes when clicking on internal links?

2) If I change the anchor to <a href="path/to/current/page/#whatever> it works, but it's just because a page refresh triggers and then the page is scrolled like normal when interpreting the hash fragment. Also, this way I loose any GET parameters (I can't predict them) which I really need since it's a database website

3) If I alter or remove the <base href="/" /> tag nothing happens, still the internal links worked before with that tag in place

4) I recently updated the .htaccess file and that could potentially be the cause but still routing has no problems and I can't see why any RewriteRule could possibly affect internal links. Also, trying to revert it to previous version didn't help

5) Same behavior applies to both Firefox and Chrome, latest versions

6) I tried to create a test page in the same environment (same .htaccess, same HTML base template) with just a very long <ul> list containing list elements with integers in sequence until 500, then a <a href="#20">To 20</a> at the bottom of the page and it just worked all good... What can force a internal link to redirect?! Please help

+0

您能向我們展示一個展示此行爲的實際頁面嗎? –

+1

我終於解決了!在這裏檢查[鏈接](http://stackoverflow.com/questions/8108836/make-anchor-links-refer-to-the-current-page-when-using-base)和在這裏[鏈接](http:// stackoverflow.com/questions/12303241/how-to-override-base-tag-without-removing-the-tag-itself)。問題出在''標籤上。我不得不使用jQuery,這裏'$( 「a.local」)上( 「點擊」,功能(E){ \t \t e.preventDefault(); \t \t document.location.hash = 「」; \t \t document.location.hash = $(this).attr(「href」); \t});' –

+1

謝謝@RadLexus,我不知道 –

回答

0

I finally solved it! Check here link和這裏link。問題出在<head>部分的<base href="/" />標籤。我用jQuery和純JavaScript的混合解決(jQuery是僅用於選擇錨,並得到屬性),在這裏

$("a.local").on("click", function (e) { 
    e.preventDefault(); 
    document.location.hash = ""; 
    document.location.hash = $(this).attr("href"); 
}); 

請注意,我需要之前,它實際上設置爲到哈希設置爲空字符串因爲document.location.hash = $(this).attr("href");單獨不會觸發內部錨點上的多次點擊自動滾動(我試過了)