2013-12-18 102 views
2

我一直在尋找在jQuery的網站,但我不覺得我很瞭解這個,因爲我沒有看到任何console.logs如何將變量傳遞給jquery.getScript?

$(document).ready(function(){ 
    var a=[1,2,3]; 
    var b='foo'; 
    var c={'bar':'baz'}; 

    $.getScript('script.js',function(a,b,c){ 
     console.log('how can i see a='+a+', b='+b+' and c='+c+' inside here?'); 
     }); 
    }); 
+0

這個作品在這裏:http://jsfiddle.net/rYQkx/沒有redifining變量作爲本地 –

回答

3

腳本is executed in the global context,所以乾脆不及格參數(他們遮蔽那些由腳本定義):

$.getScript('script.js',function(){ 
    // use a, b and c here 
    console.log(a); 
}); 
+0

都能跟得上我無法登錄任何方式醚。我做這個console.log(a);我什麼都看不到。 –

+0

@BenMuircroft因爲我猜你的變量不是全局變量。看看什麼時候變量在相同的範圍內:http://jsfiddle.net/5FCej/ –

+0

我宣佈他們在文檔準備 –