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

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

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

測試代碼打包下載

JavaScript技術return false;和e.preventDefault();的區別,轉載需保留來源!

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

主站蜘蛛池模板: 婷婷国产偷v国产偷v亚洲 | 亚洲一区二区成人 | 91精品国产免费久久久久久 | 狠狠综合| 日本道精品一区二区三区 | 欧美www| 激情综合网站 | 一区二区三区四区在线免费观看 | 国产午夜视频在线 | 欧洲色综合 | 亚色中文| 日本人妖系列 | 国产三级全黄 | 性感一级毛片 | 国产精品久久久久久福利 | 精品久久久影院 | 99国产精品欧美久久久久久影院 | 日韩在线一区二区三区视频 | 久青草国产视频 | 日本三区| 国产精品自在线拍 | 国产成人精品日本亚洲网站 | 亚洲欧洲精品成人久久曰影片 | 好吊色综合高清 | 亚洲第一黄色网址 | 亚洲视频一区在线观看 | 美女视频网站黄色 | 国产www色| 小处雏一区二区三区精 | 国产精品久久九九 | 国产三级毛片 | 国产 欧美 日产久久 | 丁香视频在线 | 天天干天天射天天爽 | 国产青草视频在线观看 | 一区二区三区免费看 | 国产91精选在线观看麻豆 | 成人视屏在线 | 国产美乳在线观看 | 久久综合视频网站 | 日本视频www|