简体   繁体   English

如何使用“返回假”; 在 Javascript(或 jQuery)中

[英]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:但也许主要是:

  • Old-style "DOM 0" handlers are generally not best practice;旧式的“DOM 0”处理程序通常不是最佳实践; instead, use modern event handling ( addEventListener , attachEvent if you need to support obsolete IE versions).相反,使用现代事件处理( addEventListenerattachEvent如果您需要支持过时的 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 falsereturn truereturn 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.

相关问题 javascript return true 或 return false 什么时候用,怎么用? - javascript return true or return false when and how to use it? 如何在jQuery / Javascript中的三元运算符中返回(true / false)? - How to return(true/false) in ternary operators in jQuery/Javascript? 是否存在“function(){return false;}”的JavaScript(或jQuery)快捷方式? - Is there a JavaScript (or jQuery) shortcut for “function(){return false;}”? Javascript / jQuery返回false不会停止执行 - Javascript / jQuery return false does not stop execution onsubmit=&quot;return false&quot; 是什么意思? (JavaScript, jQuery) - What is the meaning of onsubmit="return false"? (JavaScript, jQuery) 返回这个 | 在 JavaScript 中返回 false - Return this | return false in javaScript 当我使用“javascript: return false;”时出现“SyntaxError: return not in function” 在<a href></a> - "SyntaxError: return not in function" when I use "javascript: return false;" in <a href></a> 如何从ASP.NET中调用[Web方法]的JQuery在javascript函数中返回false - How to return false in javascript function from JQuery calling [web method] in asp.net 在确认框中单击确认或取消按钮后,如何使用JavaScript或jQuery返回true或false值? - How to return true or false values using JavaScript or jQuery after click on confirm or cancel button from confirm box? 如何在javascript中返回false停止执行? - how to stop execution with return false in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM