我正在阅读 jQuery 的 animate() 页面
http://api.jquery.com/animate/
它的示例没有提及 if using
<a href="#" id="clickme">click me</a>
...
$('#clickme').click(function() {
$('#someDiv').animate({left: "+=60"});
})
我们实际上仍然需要像以前一样返回 false 吗?
$('#clickme').click(function() {
$('#someDiv').animate({left: "+=60"});
return false;
})
(但是,这些示例没有使用 <a>
来表示“单击我”...而是使用了其他内容。
否则页面会跳回页首? jQuery 有更优雅或更神奇的方法吗?
最佳答案
您需要使用event.preventDefault() :
$('...').click(function(event) {
event.preventDefault();
// Code.
});
来自jQuery Website :
event.preventDefault()
Description: If this method is called, the default action of the event will not be triggered.
关于jquery - 使用 jQuery 的 animate(),如果点击的元素是 "<a href="#"...> </a>",该函数仍然应该返回 false 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3042036/