用params调用用户定义的函数 [英] calling user defined function with params

查看:91
本文介绍了用params调用用户定义的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义函数func1();
我希望它采取一些参数,即

  var func1(aaa,bbb){
//做些什么,什么!
};

然后我想稍后在文档中调用它:

  $('#some_id')。click(func1($ this)); 

但它不起作用。

我我一直在搞这个,但是它的奇怪之处在于jquery / javascript处理用户定义的函数的方式。有人能给我提供简单的代码片段吗? Thx

解决方案

如果您想要传递的是 $(this) ,你不需要参数,因为它可以在定义的函数中使用。

  $('#some_id') 。点击(func1的); 

但是,您不能添加这样的参数:

  $('#some_id')。click(func1(param1,param2)); 

然后,对于您的函数定义,您只需

 函数func1(){} 

如果你确实需要$(this)以外的参数,您需要执行以下操作:

  $('#some_id')。点击(函数(){
func1(aaa,bbb);});


I am defining function func1(); I want it to take some param(s) ie

var func1(aaa, bbb){
//do something, anything!
};

then i want to call it later in the doc something like this:

$('#some_id').click(func1($this));

but its not working.

I've been messing with it for a while but its very strange the way jquery/javascript handles user defined functions to me at least. Can some1 provide me with simple code snippet? Thx

解决方案

If all you want to pass is $(this), you don't need the parameter, as it will be available inside the defined function.

$('#some_id').click(func1);

However, you can't add parameters like this:

$('#some_id').click(func1(param1, param2));

Then, for your function definition, you just have

function func1() { }

If you do want parameters other than $(this), you will need to do the following:

$('#some_id').click(function () {
    func1(aaa, bbb); });

这篇关于用params调用用户定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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