一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

return false;和e.preventDefault();的區(qū)別

Have you ever seen those two things (in the title) being used in jQuery? Here is a simple example:
復(fù)制代碼 代碼如下:
$("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:
復(fù)制代碼 代碼如下:
$("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:
復(fù)制代碼 代碼如下:
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

測試代碼打包下載

JavaScript技術(shù)return false;和e.preventDefault();的區(qū)別,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: a一级一色一情 | 五月综合色啪 | 免费毛片在线视频 | 久色成人 | chinese国产在线视频 | 中国成人在线视频 | 欧美特黄高清免费观看的 | 精品精品国产高清a毛片牛牛 | 国内精品久久久久久影院老狼 | 91噜噜噜在线观看 | 日本黄色美女网站 | 国产精品亚洲片夜色在线 | 黄色视视频 | 免费精品视频在线 | 国产精品精品 | 男女一级毛片 | 国产精品免费一区二区三区 | 国产精品毛片一区二区三区 | 国产成人无精品久久久久国语 | 天天色国产 | 火辣福利视频 | 日日噜噜噜噜人人爽亚洲精品 | 国产精品自产拍视频观看 | 午夜精品久久久久久中宇 | a高清免费毛片久久 | 国产精品久久久久久五月尺 | 四虎影视永久免费观看网址 | 中文有码中文字幕免费视频 | 亚洲第一影院 | 亚洲人成一区二区三区 | 国产小视频在线免费 | 国产亚洲精品激情都市 | 欧美另类videosbestsex久久 | 日日摸夜夜添夜夜添人人爽 | 2021国内精品久久久久影院 | 欧美精品第56页在线视频观看 | 亚洲88| 日韩欧美一区二区三区 | 成人午夜免费视频 | 久久久久亚洲精品影视 | 亚洲欧洲视频在线 |