jquery - 在 IE 中点击 <a href> 标签跳转,jquery 附加到 a href

标签 jquery html internet-explorer

我有四个链接,href 指向通过 .load() 加载的页面的 URL,问题是在 IE 中,当你点击它时页面跳转,我附上了一个 (window).scrollTo(0) 到代码中,并在除 IE 之外的所有浏览器中修复它。

我还在代码上设置了return false,因此它停止了默认行为。

我看过: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();

});

最佳答案

回答我自己的问题;

发生的事情是 window.location.hash是从 <a name="hash name"> 生成的所以浏览器将页面推送到页面上的我的元素。

我所做的是从 anchor 标记中删除名称元素,并通过 jQuery 推送名称。因为我只有 4 个 anchor 标签,所以它是我的场景最简单的解决方案,但是如果你引用了很多散列标签并希望阻止它跳转,我在 Stack Overflow 上找到了另一篇帖子,其中有一些很好的答案。

Modifying document.location.hash without page scrolling

关于jquery - 在 IE 中点击 <a href> 标签跳转,jquery 附加到 a href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458462/

相关文章:

javascript - Bootstrap.js - 如何自动显示模态窗口?

javascript - jquery addClass 未按正确的顺序应用

javascript - 带有 jQ​​uery AJAX 的 PHP 函数?

javascript - 是否可以用 JS/CSS 反转 png?

html - 彼此相邻的绝对div

Javascript/Jquery : Passing numbers entered in input tag separated with space to an array

html - 在 iframe 页面内容加载时显示加载 gif

html - CSS IE 与 Firefox 中的边距问题

javascript - TinyMCE 不会在 IE 中调整图像大小

javascript - 检测 Google Earth 安装在 Internet Explorer 的网页中