JavaScript 'onclick' 事件 'return' 关键字功

JavaScript #39;onclick#39; event #39;return#39; keyword functionality(JavaScript onclick 事件 return 关键字功能)

本文介绍了JavaScript 'onclick' 事件 'return' 关键字功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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()" />

对比

<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,将中止检查的效果.因为将函数的 native 硬编码到 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.

您可以在此处查看实时示例:http://jsfiddle.net/RaBfM/1/

You can see live expamle here: http://jsfiddle.net/RaBfM/1/

这篇关于JavaScript 'onclick' 事件 'return' 关键字功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JavaScript 'onclick' 事件 'return' 关键字功

基础教程推荐