2011-06-23 108 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

相关问题