JavaScript'onclick'事件'返回'关键字功能 [英] JavaScript 'onclick' event 'return' keyword functionality
问题描述
我是javascript的新手,偶然发现了 return
关键字。基本上,这两个陈述的区别是什么?
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!
推荐答案
从函数返回false,将中止检查的效果。因为写入硬编码到html属性的函数的本机(它变成了一些新的本地函数),编写没有单词return的html将只运行该函数,并丢失其返回值,就好像你写了:
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 doAlert() {
if(some_condition)
return false;
else
return true;
}
function some_local_function() {
doAlert();
}
功能 some_local_function
赢了不返回任何值,虽然 doAlert
返回。
Function some_local_function
won't return any value, although doAlert
returns.
当你写return时,就像你写的那样第二个函数是这样的:
When you write "return", it's like you wrote the second function like this:
function some_local_function() {
return doAlert();
}
保留doAlert的返回值,无论它是什么。如果确实如此 - 操作将执行(将选中复选框) - 否则 - 它将取消。
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.
您可以在此处看到实时expamle: http://jsfiddle.net/RaBfM/1/
You can see live expamle here: http://jsfiddle.net/RaBfM/1/
这篇关于JavaScript'onclick'事件'返回'关键字功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!