Binding an existing JavaScript function in jQuery(在 jQuery 中绑定现有的 JavaScript 函数)
问题描述
使用 jQuery 我想将现有函数绑定到按钮.我浏览了文档并找到了 bind
方法,但是 jQuery 站点上的示例绑定了新创建的函数,因为我想绑定一个已经定义的函数,例如:
Using jQuery I want to bind an existing function to a button. I've been through the documentation and have found the bind
method but the examples on the jQuery site bind newly created functions where as I want to bind a function that's already defined, e.g:
function fHardCodedFunction(){
//Do stuff
}
function fBindFunctionToElement(){
$("#MyButton").bind("click", fHardCodedFunction());
}
这可能吗?还是我走错了路?
Is this possible? Or am I going about this the wrong way?
推荐答案
普通的 fHardCodedFunction
已经引用了函数,后缀 ()
只会调用它.所以只需传递函数而不是调用它,从而传递返回值:
The plain fHardCodedFunction
already refers to the function and the suffix ()
will just call it. So just pass the function instead of calling it and thereby just passing the return value:
function fBindFunctionToElement(){
$("#MyButton").bind("click", fHardCodedFunction);
}
这篇关于在 jQuery 中绑定现有的 JavaScript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jQuery 中绑定现有的 JavaScript 函数
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01