繁体   English   中英

将参数传递给jQuery函数

[英]Pass parameter to a jquery function

我知道可以这样做:

$(document).ready(testing);

function testing(){
    alert('hellow world!');
}

但是,当我想将变量传递给函数时,我将如何执行以下工作:

 $(document).ready(testing('hey world!'));

function testing(message){
    alert(message);
}

您可以使用Function.prototype.bind但是它具有一些缺点,例如丢失this引用或Event对象。

$(document).ready(testing.bind(null, 'message')); //First parameter == this;

function testing(msg){
    alert(msg); //Alert message
}

或者你可以这样做:

$(document).ready(testing.bind('message'));

function testing(){
    alert(this); //Alert message
}

小提琴: http : //jsfiddle.net/KedKq/1/

您可以使用匿名函数:

$(document).ready(function() {
  testing('hey world!'));
});

function testing(message){
    alert(message);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM