一区二区久久-一区二区三区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();的區別,轉載需保留來源!

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

主站蜘蛛池模板: 香蕉碰碰人人a久久动漫精品 | 国产在线观看91精品一区 | 国产手机在线播放 | 人人澡人人模人人添 | 亚洲激情五月 | 91福利精品老师国产自产在线 | 日本一区二区三区日本免费 | 久久久影院 | 在线欧美a| 日本在线观看www免费 | 色老板免费视频 | 在线视频观看国产 | 精品福利在线播放 | 青青国产成人久久激情91麻豆 | 在线小视频 | 色五月婷婷成人网 | 国产理论视频 | 成人精品区 | 伊人久久大香线蕉综合爱婷婷 | 色的视频网站 | 亚洲国产一级a毛片 | 五月网址 | 国产视频中文字幕 | 激情春色网 | 久久综合精品国产一区二区三区 | 国产一区国产二区国产三区 | 在线视频一区二区三区四区 | 国产成人激情 | 国产亚洲欧洲国产综合一区 | 91视频最新| 视色4se000 | 国产亚洲综合视频 | 亚洲国产成人久久综合野外 | 黄色大片网站在线观看 | 在线色网 | 国产日韩一区二区 | 四虎国产永久免费久久 | 国产综合色在线视频区色吧图片 | 日日摸人人看97人人澡 | 青青热久麻豆精品视频在线观看 | 亚洲美女网站 |