什么时候以及为什么在JavaScript中“返回false”?

时间:2021-12-31 20:19:23

When and why to return false in JavaScript?

什么时候以及为什么在JavaScript中返回false ?

11 个解决方案

#1


75  

Often, in event handlers, such as onsubmit, returning false is a way to tell the event to not actually fire. So, say, in the onsubmit case, this would mean that the form is not submitted.

通常,在事件处理程序(如onsubmit)中,返回false是一种告诉事件不实际触发的方法。例如,在onsubmit案例中,这意味着表单没有提交。

#2


64  

I'm guessing that you're referring to the fact that you often have to put a 'return false;' statement in your event handlers, i.e.

我猜你指的是你经常不得不在事件处理程序中使用“返回false”语句。

<a href="#" onclick="doSomeFunction(); return false;">...

The 'return false;' in this case stops the browser from jumping to the current location, as indicated by the href="#" - instead, only doSomeFunction() is executed. It's useful for when you want to add events to anchor tags, but don't want the browser jumping up and down to each anchor on each click

“return false;”在本例中阻止浏览器跳转到当前位置,如href="#"所示——相反,只执行doSomeFunction()。当您想要向锚标记添加事件时,它是很有用的,但是不希望浏览器在每次单击时上下跳转到每个锚点

#3


26  

It is used to stop the propagation of the event. You see when you have two elements both with a click event handler (for example)

它用于停止事件的传播。当您有两个元素同时带有一个单击事件处理程序时(例如)

-----------------------------------
| element1                        |
|   -------------------------     |
|   |element2               |     |
|   -------------------------     |
|                                 |
-----------------------------------

If you click on the inner element (element2) it will trigger a click event in both elements: 1 and 2. It is called "Event bubbling". If you want to handle the event in element2 only, then the event handler has to return false to stop the event propagation.

如果单击内部元素(element2),它将在两个元素中触发单击事件:1和2。它被称为“事件冒泡”。如果您只想处理element2中的事件,那么事件处理程序必须返回false以阻止事件传播。

Another example will be the link onclick handler. If you want to stop a link form working. You can add an onclick handler and return false. To stop the event from propagating to the default handler.

另一个例子是链接onclick处理程序。如果你想停止一个链接表单工作。您可以添加onclick处理程序并返回false。阻止事件传播到默认处理程序。

#4


18  

Er ... how about in a boolean function to indicate 'not true'?

呃…用布尔函数来表示“not true”怎么样?

#5


8  

I also came to this page after searching "js, when to use 'return false;' Among the other search results was a page I found far more useful and straightforward, on Chris Coyier's CSS-Tricks site: The difference between ‘return false;’ and ‘e.preventDefault();’

我在搜索了“js,什么时候使用'return false '之后也来到了这个页面;在其他搜索结果中,我发现在Chris Coyier的CSS-Tricks网站上有一个更有用、更直接的页面:'return false '和' e.preventDefault() '的区别;

The gist of his article is:

他文章的要点是:

function() { return false; }

函数(){返回错误;}

// IS EQUAL TO

/ / =

function(e) { e.preventDefault(); e.stopPropagation(); }

函数(e){ e.preventDefault();e.stopPropagation();}

though I would still recommend reading the whole article.

尽管我还是建议大家阅读整篇文章。

Update: After arguing the merits of using return false; as a replacement for e.preventDefault(); & e.stopPropagation(); one of my co-workers pointed out that return false also stops callback execution, as outlined in this article: jQuery Events: Stop (Mis)Using Return False.

更新:在争论使用return false的优点之后;作为e.preventDefault()的替换;& e.stopPropagation();我的一位同事指出,return false也会停止回调执行,如本文所述:jQuery Events: Stop (Mis)使用return false。

#6


6  

When using jQuery's each function, returning true or false has meaning. See the doc

使用jQuery的每个函数时,返回true或false都有意义。看到医生

#7


3  

I think a better question is, why in a case where you're evaluating a boolean set of return values, would you NOT use true/false? I mean, you could probably have true/null, true/-1, other misc. Javascript "falsy" values to substitute, but why would you do that?

我认为一个更好的问题是,为什么在计算返回值的布尔集合时,不使用true/false?我的意思是,你可能有真/空,真/-1,其他的misc。用Javascript“falsy”值替换,但为什么要这样做呢?

#8


1  

When you want to trigger javascript code from an anchor tag, the onclick handler should be used rather than the javascript: pseudo-protocol. The javascript code that runs within the onclick handler must return true or false (or an expression than evalues to true or false) back to the tag itself - if it returns true, then the HREF of the anchor will be followed like a normal link. If it returns false, then the HREF will be ignored. This is why "return false;" is often included at the end of the code within an onclick handler.

当您希望从锚标记触发javascript代码时,应该使用onclick处理程序,而不是使用javascript:伪协议。在onclick处理程序中运行的javascript代码必须返回到标记本身——如果返回true,那么锚点的HREF将像普通链接一样跟随在标记本身后面。如果返回false,则将忽略HREF。这就是“返回false”的原因,它通常包含在onclick处理程序的代码末尾。

#9


1  

You use return false to prevent something from happening. So if you have a script running on submit then return false will prevent the submit from working.

你使用return false来防止某些事情发生。因此,如果您有一个运行在提交上的脚本,那么返回false将阻止提交工作。

#10


1  

return false using only if you have some worng in function (by some test) or you want to stop some function, example use return false in end "onsubmit"

如果函数中有错误(通过某些测试)或者想要停止某个函数,例如,在onsubmit后面使用return false

#11


0  

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.

当函数中调用return语句时,该函数的执行将停止。如果指定,则将给定值返回给函数调用者。如果省略表达式,则返回undefined。

For more take a look at the MDN docs page for return.

更多信息请查看MDN文档页面。

#1


75  

Often, in event handlers, such as onsubmit, returning false is a way to tell the event to not actually fire. So, say, in the onsubmit case, this would mean that the form is not submitted.

通常,在事件处理程序(如onsubmit)中,返回false是一种告诉事件不实际触发的方法。例如,在onsubmit案例中,这意味着表单没有提交。

#2


64  

I'm guessing that you're referring to the fact that you often have to put a 'return false;' statement in your event handlers, i.e.

我猜你指的是你经常不得不在事件处理程序中使用“返回false”语句。

<a href="#" onclick="doSomeFunction(); return false;">...

The 'return false;' in this case stops the browser from jumping to the current location, as indicated by the href="#" - instead, only doSomeFunction() is executed. It's useful for when you want to add events to anchor tags, but don't want the browser jumping up and down to each anchor on each click

“return false;”在本例中阻止浏览器跳转到当前位置,如href="#"所示——相反,只执行doSomeFunction()。当您想要向锚标记添加事件时,它是很有用的,但是不希望浏览器在每次单击时上下跳转到每个锚点

#3


26  

It is used to stop the propagation of the event. You see when you have two elements both with a click event handler (for example)

它用于停止事件的传播。当您有两个元素同时带有一个单击事件处理程序时(例如)

-----------------------------------
| element1                        |
|   -------------------------     |
|   |element2               |     |
|   -------------------------     |
|                                 |
-----------------------------------

If you click on the inner element (element2) it will trigger a click event in both elements: 1 and 2. It is called "Event bubbling". If you want to handle the event in element2 only, then the event handler has to return false to stop the event propagation.

如果单击内部元素(element2),它将在两个元素中触发单击事件:1和2。它被称为“事件冒泡”。如果您只想处理element2中的事件,那么事件处理程序必须返回false以阻止事件传播。

Another example will be the link onclick handler. If you want to stop a link form working. You can add an onclick handler and return false. To stop the event from propagating to the default handler.

另一个例子是链接onclick处理程序。如果你想停止一个链接表单工作。您可以添加onclick处理程序并返回false。阻止事件传播到默认处理程序。

#4


18  

Er ... how about in a boolean function to indicate 'not true'?

呃…用布尔函数来表示“not true”怎么样?

#5


8  

I also came to this page after searching "js, when to use 'return false;' Among the other search results was a page I found far more useful and straightforward, on Chris Coyier's CSS-Tricks site: The difference between ‘return false;’ and ‘e.preventDefault();’

我在搜索了“js,什么时候使用'return false '之后也来到了这个页面;在其他搜索结果中,我发现在Chris Coyier的CSS-Tricks网站上有一个更有用、更直接的页面:'return false '和' e.preventDefault() '的区别;

The gist of his article is:

他文章的要点是:

function() { return false; }

函数(){返回错误;}

// IS EQUAL TO

/ / =

function(e) { e.preventDefault(); e.stopPropagation(); }

函数(e){ e.preventDefault();e.stopPropagation();}

though I would still recommend reading the whole article.

尽管我还是建议大家阅读整篇文章。

Update: After arguing the merits of using return false; as a replacement for e.preventDefault(); & e.stopPropagation(); one of my co-workers pointed out that return false also stops callback execution, as outlined in this article: jQuery Events: Stop (Mis)Using Return False.

更新:在争论使用return false的优点之后;作为e.preventDefault()的替换;& e.stopPropagation();我的一位同事指出,return false也会停止回调执行,如本文所述:jQuery Events: Stop (Mis)使用return false。

#6


6  

When using jQuery's each function, returning true or false has meaning. See the doc

使用jQuery的每个函数时,返回true或false都有意义。看到医生

#7


3  

I think a better question is, why in a case where you're evaluating a boolean set of return values, would you NOT use true/false? I mean, you could probably have true/null, true/-1, other misc. Javascript "falsy" values to substitute, but why would you do that?

我认为一个更好的问题是,为什么在计算返回值的布尔集合时,不使用true/false?我的意思是,你可能有真/空,真/-1,其他的misc。用Javascript“falsy”值替换,但为什么要这样做呢?

#8


1  

When you want to trigger javascript code from an anchor tag, the onclick handler should be used rather than the javascript: pseudo-protocol. The javascript code that runs within the onclick handler must return true or false (or an expression than evalues to true or false) back to the tag itself - if it returns true, then the HREF of the anchor will be followed like a normal link. If it returns false, then the HREF will be ignored. This is why "return false;" is often included at the end of the code within an onclick handler.

当您希望从锚标记触发javascript代码时,应该使用onclick处理程序,而不是使用javascript:伪协议。在onclick处理程序中运行的javascript代码必须返回到标记本身——如果返回true,那么锚点的HREF将像普通链接一样跟随在标记本身后面。如果返回false,则将忽略HREF。这就是“返回false”的原因,它通常包含在onclick处理程序的代码末尾。

#9


1  

You use return false to prevent something from happening. So if you have a script running on submit then return false will prevent the submit from working.

你使用return false来防止某些事情发生。因此,如果您有一个运行在提交上的脚本,那么返回false将阻止提交工作。

#10


1  

return false using only if you have some worng in function (by some test) or you want to stop some function, example use return false in end "onsubmit"

如果函数中有错误(通过某些测试)或者想要停止某个函数,例如,在onsubmit后面使用return false

#11


0  

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.

当函数中调用return语句时,该函数的执行将停止。如果指定,则将给定值返回给函数调用者。如果省略表达式,则返回undefined。

For more take a look at the MDN docs page for return.

更多信息请查看MDN文档页面。