[英]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.