色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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)系我們修改或刪除,多謝。

主站蜘蛛池模板: 连山| 泾阳县| 关岭| 萨迦县| 台南市| 牡丹江市| 营山县| 长宁县| 修水县| 即墨市| 临湘市| 资兴市| 青神县| 安远县| 理塘县| 简阳市| 丹东市| 桑植县| 永春县| 读书| 长垣县| 当阳市| 巩留县| 台东县| 塔城市| 沾益县| 汝州市| 贵州省| 防城港市| 夏邑县| 改则县| 连州市| 睢宁县| 石屏县| 敖汉旗| 五大连池市| 天峨县| 北流市| 新乡县| 临清市| 诸暨市|