return false;和e.preventDefault();的區(qū)別
$("a").click(function() {
$("body").append($(this).attr("href"));
return false;
}
That code would append the href attribute as text to the body every time a link was clicked but not actually go to that link. The return false; part of that code prevents the browser from performing the default action for that link. That exact thing could be written like this:
$("a").click(function(e) {
$("body").append($(this).attr("href"));
e.preventDefault();
}
So what's the difference?
The difference is that return false; takes things a bit further in that it also prevents that event from propagating (or “bubbling up”) the DOM. The you-may-not-know-this bit is that whenever an event happens on an element, that event is triggered on every single parent element as well. So let's say you have a box inside a box. Both boxes have click events on them. Click on the inner box, a click will trigger on the outer box too, unless you prevent propagation. Like this:

演示地址:http://css-tricks.com/examples/ReturnFalse/
So in other words:
function() {
return false;
}
// IS EQUAL TO
function(e) {
e.preventDefault();
e.stopPropagation();
}
It's all probably a lot more complicated than this and articles like this probably explain it all a lot better.
參考:
1.The difference between ‘return false;' and ‘e.preventDefault();'
2.Event order
測試代碼打包下載
相關(guān)文章
javascript顯示系統(tǒng)當(dāng)前時間代碼
這篇文章主要為大家詳細(xì)介紹了javascript如何顯示系統(tǒng)當(dāng)前時間代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12微信小程序wx.uploadfile 本地文件轉(zhuǎn)base64的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序wx.uploadfile 本地文件轉(zhuǎn)base64的實(shí)現(xiàn)方法,文中通過代碼講解給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06js+html+css實(shí)現(xiàn)簡單日歷效果
這篇文章主要為大家詳細(xì)介紹了js+html+css實(shí)現(xiàn)簡單日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06如何利用JavaScript實(shí)現(xiàn)排序算法淺析
排序算法是筆試中經(jīng)常出現(xiàn)的,提起排序算法就一定要提下算法復(fù)雜度和大O表示法,算法復(fù)雜度是指算法在編寫成可執(zhí)行程序后,運(yùn)行時所需要的資源,資源包括時間資源和內(nèi)存資源,這篇文章主要給大家介紹了關(guān)于如何利用JavaScript實(shí)現(xiàn)排序算法的相關(guān)資料,需要的朋友可以參考下2021-11-11JS實(shí)現(xiàn)簡單的頂部定時關(guān)閉層效果
這篇文章主要介紹了通過JS實(shí)現(xiàn)的簡單頂部定時關(guān)閉層效果,需要的朋友可以參考下2014-06-06javascript 動態(tài)table添加colspan\rowspan 參數(shù)的方法
動態(tài)的給某個表對象添加列屬性和行屬性,采用obj.setAttribute("rowspan",n)(即rowspan=n)不能生效。2009-07-07學(xué)習(xí)LayUI時自研的表單參數(shù)校驗(yàn)框架案例分析
本框架基于LayUI框架寫的表單參數(shù)校驗(yàn)框架,本文分過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧2019-07-07