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

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

主站蜘蛛池模板: 中文字幕一区二区日产乱码 | 91网页版| 最新国产视频 | 日韩一级特黄 | 色妹子综合 | 91久久国产成人免费观看资源 | 黄色在线免费观看网址 | 国产精品露脸国语对白手机视频 | 久久国产亚洲 | 婷婷综合国产激情在线 | 综合天堂| 欧美黄视频网站 | 亚洲人成一区二区三区 | 国内成人精品亚洲日本语音 | 41sao.can在线观看国产 | 色www永久免费网站国产 | 成人国产精品免费网站 | 起碰免费视频97网站 | 久国产精品久久精品国产四虎 | 欧美视频福利 | 亚洲天堂一区二区在线观看 | 护士精品一区二区三区 | 大色香蕉 | 丁香激情综合 | 免费在线观看黄视频 | 国产在线观看91精品2022 | 国产精品第1页在线播放 | 国产精品欧美一区二区三区不卡 | 狠狠综合久久综合鬼色 | 一区二区三区四区在线不卡高清 | 欧美特级视频 | 国产一区二区高清在线 | 女人被免费视频网站 | 华人亚洲欧美精品国产 | 国产午夜精品视频 | 精品久久久久久综合日本 | 香港三级欧美国产精品 | 中日韩美中文字幕 | 国产一级久久免费特黄 | 日韩精品123| 国产精品日韩欧美一区二区三区 |