JavaScript #39;onclick#39; event #39;return#39; keyword functionality(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' 关键字功
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01