簡體   English   中英

將參數傳遞給Javascript中的函數

[英]Passing an argument to a function in Javascript

我有一個按鈕,每次單擊時都會添加一個段落。 在我的代碼中,我有一個名為addPara(count)的函數,並且我有一個counterstart變量。 我如何將此counterstart變量傳遞給我的功能?

addPara(counterstart);

如果您不確定不確定要傳遞多少個參數,則可以通過函數內部的arguments集合引用任何和所有參數。

function addPara() {
    console.log(arguments); // "arguments" is a collection of the arguments.
    if(arguments[0] === undefined) { 
         // do something and return if no arguments were given
        return false;
    }
      // otherwise loop through the arguments passed
    for( var i = 0, len = arguments.length; i < len; i++ ) {
       // do something with arguments[ i ];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM