2013-06-21 131 views
0

我對此有點新,但我不明白爲什麼我的jQuery腳本沒有執行。這是我main.js裏面的函數,我調用它像這樣:Jquery腳本不能在頁面加載執行

jQuery(function($){ 
    geoCode(); 
}) 

http://jsfiddle.net/spadez/tZNw4/2/

我希望拿出在頁面加載的警報,但實際上它並不做任何事情。任何人都可以告訴我我做錯了什麼?

+1

'U ncaught SyntaxError:意外的標記)'。 [瞭解如何**調試** JavaScript。](http://www.netmagazine.com/tutorials/javascript-debugging-beginners)。 –

回答

0

嘗試的document.ready:

$(document).ready(function() { 
    geoCode(); 
}); 

編輯:啊,他們這樣做(TIL)。

固定它爲您提供:

http://jsfiddle.net/tZNw4/18/

+1

'$(function(){...});'與'$(document).ready(function(){...})完全相同;',它只是簡寫。 –

0

我認爲你應該使用的文檔準備事件。

jQuery(document).ready(function() { 
    geoCode(); 
}); 
+2

他們已經是,他們只是用它的簡寫語法。 –

+0

哦,謝謝...我討厭jQuery(函數($){..});將在解析器到達時執行一次。 (所以在頁面渲染過程中..)。 – Marco

+0

好吧,它只會在DOM準備好時回調函數。 –

1

你的代碼應該是在文件準備像

$(document).ready(function() { 
    geoCode(); 
}); 

而且看到這個FIDDLE你給us.You把多餘的右括號「)」末

+1

他們已經在使用簡寫語法,準備文件不是問題。 –

+0

而且我提到了大括號問題 – Gautam3164

1

刪除最後的右括號在JavaScript窗口中。

1

你的問題是在這裏的功能:

function geoCode() { 
    alert("test"); 
}); //<------------------see here the closing `);` 

使它像這樣:

function geoCode() { 
    alert("test"); 
}