Javascript Jquery 事件处理程序返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4379403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery event handlers return values
提问by Buzzzz
Is there any use to return values from .click()
and .change()
handlers (like return true
or return false
)?
从.click()
和.change()
处理程序(如return true
或return false
)返回值是否有任何用处?
回答by Peter Bailey
return false;
will stop the event from bubbling up ANDsuppress the default action.
return false;
将向上冒泡停止事件和镇压的默认操作。
In otherwords, returning false is analogous to these two lines
换句话说,返回 false 类似于这两行
event.stopPropagation();
event.preventDefault();
For events that bubble and are cancelable, at least.
至少对于冒泡且可取消的事件。
回答by jwueller
return false
in such a handler results in event.stopPropagation()
(only for jQuery event handlers, as Tim Down suggested in the comments) and event.preventDefault()
being called automatically by jQuery.
return false
在这样的处理程序中导致event.stopPropagation()
(仅适用于 jQuery 事件处理程序,正如 Tim Down 在评论中建议的那样)event.preventDefault()
并由 jQuery 自动调用。
return true
is the same as returning nothing (no action is taken by jQuery).
return true
与不返回任何内容相同(jQuery 不执行任何操作)。