2017-09-01 37 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的原始代码中,在初始插件定义的结束大括号之后有一个不必要的“)”。看起来你也抓到了。干杯。 –

相关问题