在jQuery中绑定现有的JavaScript函数 [英] Binding an existing JavaScript function in jQuery

查看:90
本文介绍了在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 bind newly created functions where as I want to bind a function that's already hard coded, 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函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆