簡體   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