すべてのjqueryコードを含むjsファイルがあります。2つの方法に従いましたが、どちらが優れているかわかりません。
初め:
jQuery(document).ready(function(){
var $ = jQuery;
//some code here
//another code not related to the first one
//also another independent code
//... and so on
});
2番:
jQuery(document).ready(function(){
//call the functions here
my_func_1();
my_func_2();
my_func_3();
my_func_4();
});
//list of functions
function my_func_1() {
//function code
}
function my_func_2() {
//function code
}
function my_func_3() {
//function code
}
function my_func_4() {
//function code
}
2番目のメソッドはより適切で整理されているように見えますが、my_func_2()がページ上で探しているものを見つけられなかったとしましょう。たとえば、$('#my-id')、my_func_2()に続く関数は実行されません。
また、別の方法を試し、すべてのjquery関数を1つのjsファイルで定義してから、HTMLのスクリプトタグを使用して関数を追加しました。
<script>my_func_2();</script>
では、jqueryコードをグループ化するための最良の方法は何ですか?
使用する必要があります:
jQuery(document).ready(function(){
});
コードの束ごとに?
よろしくお願いします。