jquery - 使用 jQuery 的 animate(),如果点击的元素是 "<a href="#"...> </a>",该函数仍然应该返回 false 吗?

标签 jquery event-handling jquery-events dhtml dynamic-html

我正在阅读 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/

相关文章:

javascript - .unbind().click(function() {...}) 是一个好习惯吗?

javascript - 如果 DateTime 在过去,jQuery TimeAgo 库会更改文本的颜色?

javascript - 为什么 .next() 返回事件对象本身

带有 keyTyped 事件的 Java 获取操作命令

javascript - window.load 和 document.ready 中的 jQuery data() 未定义?

javascript - 如何在下拉列表的 anchor 标记上触发鼠标悬停事件

jquery - 如何拥有清晰、锐利和 slim 的 jqueryUI GUI 元素?

jquery - 如何让 jQuery Sticky Float 插件 react 动态页面高度变化?

Javascript onclick 提交按钮问题

c# - 如何跨对象链处理事件?