javascript - 事件声明中是否需要“function()”?
问题描述
我有两个脚本:
<script src="../js/createopp.min.js"></script>
<script src="../js/validation.min.js"></script>
以下行在第一个脚本中,在另一个脚本中调用了一个函数:
$('#places').blur(function(){checkPlacesAvailable()});
如果我将其重写为
$('#places').blur(checkPlacesAvailable());
我在函数上收到“未定义”错误。但是我看到很多使用第二种格式的例子。是否有明显的东西阻止第二个版本在我的情况下工作?
解决方案
You should pass function references to event handlers like that.
$('#places').blur(function(){checkPlacesAvailable()});
"is" "equivalent" to this
$('#places').blur(checkPlacesAvailable);
Note that it is not exactly the same because of scopes and all that but most of the times you can pass it in the second manner
推荐阅读
- javascript - 无法在 Leaflet 中使用过滤器选项
- python - 遍历到基类
- c# - 如何使用 C# 在 ASP.NET 中中止请求,如 IIS 请求中止
- python - 如何动态提供文件,然后让它们在 Django 中可下载?
- gem5 - 在 gem5 中恢复检查点时,我可以更改哪些系统特征,例如缓存配置的核心数?
- angular - Angular 绑定复选框到 BehaviorSubject
- python - 如何在 VS Script 中上传或构建文件?
- python-3.x - 在导入的库中加载环境变量时如何将导入保持在文件顶部?
- php - 如何一起使用 tax_query 和 meta_query?
- android - 如何使用 EXPLORE-NFC-WW 从带有 React-Native 设备的 Android 向 RaspberryPi 发送一个简单的字符串