[英]How to use “return false;” in Javascript (or jQuery)
I was wondering about the use of return false;
我想知道
return false;
的使用return false;
because…因为……
⋅ Sometimes I see functions like that: ⋅ 有时我会看到这样的函数:
function foo(){
⋅⋅⋅
return false;
}
⋅ Sometimes I see the calling of functions like that: ⋅ 有时我会看到这样的函数调用:
<div onclick="foo(); return false;"></div>
⋅ Sometimes I see both of the above, ⋅ 有时我同时看到以上两种情况,
⋅ Sometimes I see none of the above. ⋅有时我看到以上皆非。
(I know about the topic What's the effect of adding 'return false' to a click event listener? , but that seems to answer only to my point #2, or am I wrong?) (我知道主题将“返回假”添加到点击事件侦听器有什么效果? ,但这似乎只回答了我的第 2 点,还是我错了?)
So I ended up with questions about it.所以我最终提出了关于它的问题。
What is the right way to use return false;
使用
return false;
的正确方法是什么? ? ?
And when is it necessary to do so?什么时候需要这样做?
When using jQuery, is the right way or the necessity modified ?使用 jQuery 时,是正确的方法还是修改了必要性?
Thanks in advance for any answer.提前感谢您的任何回答。
In an old-style onxyz
-attribute event handler (commonly called "DOM 0" because they're not specified), returning false
prevents the default action (more in this question ).在旧式
onxyz
-attribute 事件处理程序(通常称为“DOM 0”,因为它们未指定)中,返回false
阻止默认操作(在此问题中有更多内容)。 For instance, for a link, it prevents following the link;例如,对于链接,它会阻止跟随链接; for a form's
onsubmit
, it prevents form submission.对于表单的
onsubmit
,它会阻止表单提交。
So there are multiple answers to your "what's the correct way" question:因此,您的“正确方法是什么”问题有多种答案:
If you want the function foo
to determine whether the default action occurs, use onclick="return foo()"
and have foo
return false
to prevent the default, or return anything else (or just return;
, which will effectively return undefined
) to allow it.如果您希望函数
foo
确定是否发生默认操作,请使用onclick="return foo()"
并让foo
return false
以防止默认操作,或者返回任何其他内容(或仅return;
,这将有效地返回undefined
)到允许。
If you always want the default prevented, use onclick="foo(); return false;"
如果您总是希望阻止默认设置,请使用
onclick="foo(); return false;"
(although that will fail to prevent the default if foo
throws an exception). (尽管如果
foo
抛出异常,这将无法阻止默认值)。
On any vaguely-modern browser, if you always want the default prevented even if foo
throws an exception: onclick="event.preventDefault(); foo();"
在任何模糊的现代浏览器上,如果即使
foo
抛出异常,您也始终希望阻止默认值: onclick="event.preventDefault(); foo();"
(yes, this even works on Firefox, which doesn't have the global event
variable — because onxyz
handlers execute in a scope with a local event
variable). (是的,这甚至适用于没有全局
event
变量的 Firefox — 因为onxyz
处理程序在具有本地event
变量的范围内执行)。
But perhaps mostly:但也许主要是:
addEventListener
, attachEvent
if you need to support obsolete IE versions).addEventListener
, attachEvent
如果您需要支持过时的 IE 版本)。 Take your choice.做出你的选择。 :-)
:-)
First, I do not recommend inline JavaScript.首先,我不推荐内联 JavaScript。
You should use addEventListener
.您应该使用
addEventListener
。
Example :示例:
document.getElementById('yourdiv').addEventListener('click', function(){
foo();
return false;
});
OR或
document.getElementById('yourdiv').addEventListener('click', function(){
foo();
});
with function foo() { return false }
使用
function foo() { return false }
depending on your needs根据您的需要
First case :第一种情况:
function foo() {
return false;
}
The function foo
is called, and this very function returns false
.函数
foo
被调用,这个函数返回false
。 So you can get the false by doing :所以你可以通过做得到假:
var isitfalse = foo()
--> isitfalse
will be defined to false
var isitfalse = foo()
--> isitfalse
将被定义为false
OR或
if (!isitfalse) {
alert('Yes it\'s false')
}
Second case :第二种情况:
<div onclick="foo(); return false;"></div>
The action onclick
will call the foo
function, then onclick
will call return false
.动作
onclick
将调用foo
函数,然后onclick
将调用return false
。 So it's different from the first case where it's your function that returns false
所以它与第一种情况不同,第一种情况是你的函数返回
false
jQuery jQuery
return false
or return true
or return what-ever-you-want
works the same in jQuery, same way to use it return false
或return true
或return what-ever-you-want
在 jQuery 中的工作方式相同,使用方式相同
Other thing to know :其他须知:
If you ask a function to return anything, the function won't go further.如果你要求一个函数返回任何东西,这个函数就不会走得更远。
function foo() {
if (2 > 1) { return something }
alert()
}
In this case, you'll never see the alert在这种情况下,您将永远不会看到警报
Functions with no explicit return value will always return undefined
.没有明确返回值的函数将始终返回
undefined
。 In case of event handlers, you return false to avoid the default browser behavior.在事件处理程序的情况下,您返回 false 以避免默认浏览器行为。 See What's the effect of adding 'return false' to a click event listener?
请参阅将“返回假”添加到单击事件侦听器的效果是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.