2017-09-01 44 views
0

我想弄清楚爲什麼我不能將字符串傳遞到包含另一個函數的函數中。當我警覺時,我得到undefined如何將字符串傳遞到jQuery中的嵌套函數

$.fn.downCount = function (current_time) { 
    function countdown(current_time) { 
     alert(current_time) 
    } 
}) 

var current_time = "01:00"; 
downCount(current_time); 
+0

但這些代碼不會警告任何東西。無論如何,如果你想使用'downCount'中的'current_time',那麼''current_time'是'countdown'的參數。 – Li357

回答

1

你從來沒有真正調用內部函數。調用函數並傳入current_time

$.fn.downCount = function (current_time) { 
 
    function countdown() { 
 
     alert(current_time) 
 
    } 
 
    countdown(); 
 
} 
 

 

 
var current_time = "01:00"; 
 
$.fn.downCount(current_time);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
...

此外,作爲安德魯提到的,你不需要在current_time通入countdown功能。它可以簡化爲:

$.fn.downCount = function (current_time) { 
    function countdown() { 
     alert(current_time) 
    } 
    countdown(); 
} 
+1

打敗我吧。我還想補充一點,在OP的原始代碼中,在初始插件定義的結束大括號之後有一個不必要的「)」。看起來你也抓到了。乾杯。 –

相關問題