event.preventDefault() 和 return false 的工作方式不同,当出现问题时存在差异(阅读:错误)以及 event.preventDefault() 是否已经执行,这里是一个重要的例子和为什么 return false 并不总是有效:
$('a').click( function(e){
$(this).css("background", "green");
err(); // err is not defined, so JavaScript will throw error, and the browser will ignore rest of the code (return false;). The browser will perform the default action.
return false;
});
$('a').click( function(e){
e.preventDefault();
$(this).css("background", "green");
err(); // err is not defined, so JavaScript will throw error, and the browser will ignore rest of the code. We already called event.preventDefault() so the browser will not perform default action.
});