我有四个链接,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 上找到了另一篇帖子,其中有一些很好的答案。
关于jquery - 在 IE 中点击 <a href> 标签跳转,jquery 附加到 a href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6458462/