2013-02-12 213 views
1

我想知道兩者之間的區別。jquery文檔準備問題

(function($){ 
    //some console.log code  
});
$(document).ready(function() 
{ 
    //some console.log code 
});

你們會罵我傻,但我不知道爲什麼它的發生。

那麼這裏是問題。

當我使用(function($){然後我看不到任何console.log的結果,但它顯示當我使用document.ready所有console debug結果。我正在使用jQuery v1.8.2

謝謝。

+0

採取看看這裏:http://stackoverflow.com/questions/3908724/what-event-does-jquery-function-fire-on。 – 2013-02-12 05:58:22

回答

1

你錯過了在閉幕式在第一個例子中的東西:

(function($){ 
    //some console.log code  
})(jQuery); // <----------add (jQuery) here and test it 

或本:

jQuery(function($){ // <---------add jQuery first here 
    //some console.log code  
    }); 
+0

絕對正確 – 2013-02-12 05:18:34

1

代碼

(function($){ 
    //some console.log code  
}); 

應該是這樣的

$(function() { 
    //some console.log code  
}); 

現在測試一下。

參考http://api.jquery.com/ready/

2

第一個

$(function(){...}); //missing $ sign here in your code 

日是隻是一個shorcut打電話document.ready在jquery .. 兩者是完全一樣的.. 如果你碰巧看到核心..你會注意到這在一條評論... here是鏈接