2011-06-23 106 views
3

I have four links with href's pointing to URLs of pages that are loaded via .load(), the problem is in IE the page jumps when you click it, i have attached a (window).scrollTo(0) to the code and fixes it in all browsers besides IE.頁面跳轉點擊<a href> tag in IE, jquery is attached to the a href

I also have return false on the code so it stops the default behaviour.

I have seen: Page jumps to the top onclick並試圖實現答案,但它似乎並不適用於我。

有誰知道解決方案嗎?

在href的:

<a href="welcome.html" name="welcome">Welcome</a> 
<a href="about.html" name="about">About</a> 
<a href="forum.html" name="forum">Forum</a> 
<a href="contact.html" name="contact">Contact</a> 

jQuery代碼:

$('#jqNav li a').click(function(e){ 

    if($(this).parent().is(".nav1")){ $('.landing .main .nav ul').css({ "background-position" : "0 -50px" });} 
    else if($(this).parent().is(".nav2")) { $('.landing .main .nav ul').css({ "background-position" : "0 -100px" });} 
    else if($(this).parent().is(".nav3")) { $('.landing .main .nav ul').css({ "background-position" : "0 -150px" });} 
    else if($(this).parent().is(".nav4")) { $('.landing .main .nav ul').css({ "background-position" : "0 -200px" });}; 

    stopAnim = true; 
    $page = $(this).attr('href'); 
    var $hashTag = $(this).attr('name'); 
    window.location.hash = $hashTag; 
    loadData(); 
    $(window).scrollTop(0); 
    e.preventDefault(); 

}); 
+0

那麼你在說什麼代碼? – Sparky

+0

虐待添加到OP現在:) – Xavier

+0

什麼在'loadData'?如果它是異步代碼,那可能是問題。 – lonesomeday

回答

0

要回答我自己的問題;

發生了什麼是window.location.hash是從<a name="hash name">生成的,因此瀏覽器正在將頁面推送到頁面上的元素。

我所做的是從定位標記中刪除名稱元素,並通過jQuery將名稱推入。由於我只有4個錨標籤,它是我的方案的最簡單的解決方案,如果你引用大量的哈希標籤,並希望阻止它跳躍,我發現另一篇文章堆棧溢出與一些很好的答案。

Modifying document.location.hash without page scrolling

相關問題