JavaScript“onclick”事件“返回”关键词功能
我对 javascript 很陌生,偶然发现了 return
关键字。基本上,这两种说法有什么区别?
<input type="checkbox" onclick="doAlert()" />
vs
<input type="checkbox" onclick="return doAlert();" />
本质上,两者都返回相同的结果并调用该函数,但是还有更多吗?非常感谢任何帮助:)。谢谢!
I am really new to javascript, and stumbled upon the return
keyword. Basically, what is the difference in terms of these 2 statements?
<input type="checkbox" onclick="doAlert()" />
vs
<input type="checkbox" onclick="return doAlert();" />
Essentially, both returned the same results and called the function, but is there more to it? Any help greatly appreciated :). Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
某些 html 元素具有在返回 true/false 时表现不同的 JS 事件。例如:
...vs...
在第二个实例中,如果 ValidateForm 函数返回 false,则表单将不会提交,在第一个实例中,即使函数返回 false,表单仍会提交。
我认为在这种情况下,您可以看到使用
return
关键字和不使用return
关键字之间的区别。更新为了简化,如果您使用
return
关键字,您会将一个值传递回调用onsubmit
的函数。如果没有它,您只需调用您在事件处理程序中命名的函数,并且不会返回任何内容。更新 2021-01-21 此功能也适用于 html 锚点/链接 (
a
) 上的onclick
方法:示例用法:
Click Me
Some html elements have JS events that behave differently when true/false is returned. For instance:
...vs...
In the second instance, if the
ValidateForm
function returned false the form will not submit, in the first even if the function returns false the form will still submit.I think this scenario, you can see the different between using the
return
keyword and not.UPDATED To simplify, if you use the
return
keyword you are passing a value back to the function that called theonsubmit
. Without it, you are simply calling the function that you name in the event handler and do not return anything.UPDATE 2021-01-21 This functionality also work for the
onclick
method on html anchors / links (a
):Sample Usage:
Click Me
从函数返回 false,将中止检查的效果。因为本机函数被硬编码到 html 属性中(它变成了一些新的本地函数),所以编写没有“return”一词的 html 只会运行该函数,并丢失其返回值,就像您编写的那样:
Function <尽管
doAlert
返回,但 code>some_local_function 不会返回任何值。当你写“return”时,就像你写了第二个函数一样:
它保留了 doAlert 的返回值,无论它是什么。如果为真 - 操作将执行(复选框将被选中) - 否则 - 它将取消。
您可以在此处查看实时说明:http://jsfiddle.net/RaBfM/1/
Returning false from the function, will abort the effect of the checking. Because the native of functions that written hardcoded into html properties (it became some new local function), writing the html without the word "return" will just run the function, and lose its returning value, as if you've wrote:
Function
some_local_function
won't return any value, althoughdoAlert
returns.When you write "return", it's like you wrote the second function like this:
which preserves the returning value of doAlert, whatever it will be. If it's true - the action will perform (the checkbox will be checked) - otherwise - it will cancel.
You can see live expamle here: http://jsfiddle.net/RaBfM/1/
添加
return
到一个function(),它会禁用浏览器的默认行为,它会禁用submit
的工作。意味着如果函数返回 false,它将使您保持在同一页面,并且您可以再次将值填充到字段中。如果你没有将
return
与function()一起使用,那么submit
函数将执行它的工作,它将重定向到下一个指定的页面,而不关心返回的结果是否是是真是假。adding
return
to a function(), it disables the default behaviour of the browser, it disables the job ofsubmit
. Means it will keep you on the same page if the function returns false and you can fill up the value into the field again.If you do not use
return
with the function() thensubmit
function will perform its job, it will redirect to the next specified page, without caring about the returned result whether it was true or false.