如何防止锚点跳动? [英] How to prevent jump on an anchor click?
本文介绍了如何防止锚点跳动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用e.preventDefault();
禁用默认锚定行为.
I use e.preventDefault();
to disable default anchor behaviour.
有没有一种方法可以防止单击时仅跳转到目标?
Is there a way to prevent only jump action to a target on click?
我尝试过:
var hash = window.location.hash;
var link = $('a');
$('a').click(function() {
e.preventDefault();
hash = "#"+link.attr("href");
});
但是它不起作用: http://jsfiddle.net/ynts/LgcmB/
推荐答案
$(document).ready(function() {
var hash = window.location.hash;
var link = $('a');
$('a').click(function(e) {
e.preventDefault();
hash = link.attr("href");
window.location = hash;
});
});
还必须在函数中指定事件参数.通过将其命名为e
或event
,然后您就可以对其进行操作.
You also have to specify event argument in the function. By naming it as e
or event
and then you can manipulate it.
这篇关于如何防止锚点跳动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文