Using jQuery's animate(), if the clicked on element is “<a href=”#“ …> </a>”, the function should still return false?

馋奶兔 提交于 2019-12-23 20:10:08

问题


I was reading jQuery's page for animate()

http://api.jquery.com/animate/

Its examples don't mention about if using

<a href="#" id="clickme">click me</a>
...

$('#clickme').click(function() {
    $('#someDiv').animate({left: "+=60"});
})

we actually still have to return false like in the old days?

$('#clickme').click(function() {
    $('#someDiv').animate({left: "+=60"});
    return false;
})

(but then, those examples didn't use a <a> for the "click me"... but used something else.

Otherwise the page will jump back to the beginning of the page? Does jQuery have a more elegant or magical way of doing it?


回答1:


You need to use event.preventDefault():

$('...').click(function(event) {
    event.preventDefault();
    // Code.
});

From the jQuery Website:

event.preventDefault()
Description: If this method is called, the default action of the event will not be triggered.



来源:https://stackoverflow.com/questions/3042036/using-jquerys-animate-if-the-clicked-on-element-is-a-href-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!