return false;和e.preventDefault();的区别
来源:互联网 发布:手机上怎么查淘宝等级 编辑:程序博客网 时间:2024/04/30 11:22
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 butnot 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:
This demo.
So in other words:
function() { return false;}// IS EQUAL TOfunction(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.
- return false;和e.preventDefault();的区别
- return false和e.preventDefault()的区别
- return false 和 e.preventDefault 的区别
- preventDefault()、 preventDefault()和 return false 的区别
- stopPropagation();和 return false;和e.preventDefault;的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- stopPropagation, preventDefault 和 return false 的区别
- matlab下如何调试c函数
- Matlab调用c程序
- 地址总线,字长,内存容量,寻址范围 之间的计算
- 运行asmcmd 命令报错
- 33句爱的真言,有人看了第一句就哭了
- return false;和e.preventDefault();的区别
- Supported and Compatible Compilers – Release 2010b
- 如何学好C语言—酷壳
- 期权讲解—— 邵亦波:别错过成为千万富翁机会
- USACO Section 3.3 Home on the Range - 优化的BFS..
- Self-learning:Overview of Updating a Windows Azure Service
- 深入理解C语言—酷壳
- 2012学习计划
- 人脸识别技术资料和源码(包含人脸库)