jQuery(function($){//alert($);var index = 0;var maximg = 5;//$('<div id="flow"></div>').appendTo("#myjQuery");//滑動導航改變內容 $("#myjQueryNav li").hover(function(){if(MyTime){clearInterval(MyTime);}index = $("#myjQueryNav li").index(this);MyTime = setTimeout(function(){ShowjQueryFlash(index);$('#myjQueryContent').stop();} , 400);}, function(){clearInterval(MyTime);MyTime = setInterval(function(){ShowjQueryFlash(index);index++;if(index==maximg){index=0;}} , 3000);});//滑入 停止動畫,滑出開始動畫.$('#myjQueryContent').hover(function(){if(MyTime){clearInterval(MyTime);}},function(){MyTime = setInterval(function(){ShowjQueryFlash(index);index++;if(index==maximg){index=0;}} , 3000);});//自動播放var MyTime = setInterval(function(){ShowjQueryFlash(index);index++;if(index==maximg){index=0;}} , 3000);});function ShowjQueryFlash(i) {$("#myjQueryContent div").eq(i).animate({opacity: 1},1000).css({"z-index": "1"}).siblings().animate({opacity: 0},1000).css({"z-index": "0"});//$("#flow").animate({ left: 652+(i*76) +"px"}, 300 ); //滑塊滑動$("#myjQueryNav li").eq(i).addClass("current").siblings().removeClass("current");}
2 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
實際上,jquery只是js寫出來的對象,或者稱工廠(產生新的對象)
jquery源碼中的定義可以理解為 var jQuery = $ = function($){ } (jQuery)
即,function參數為形參,function后的括號內的內容為實參,實參賦值給形參
在你給出的例子中,可以斷言肯定已經引入了jquery,故其實$已經被賦值為jQuery對象,因此這個函數是將jquery作為參數傳進函數內部,作為jquery的代名詞。
可能你要問為什么要這么做,直接用$不是很好么?這有一種可能是不止引入了jquery,還引入了prototype(也是使用$作為工廠符號)之類的,為防混淆如此做。
這樣做其實是因為js編程中的封裝,防止變量污染其他作用域,使得$只作用于這個函數。
但我感覺這樣寫的不是很好,如果換成
jQuery( function (jQuery){ var $ = jQuery; //alert($); var index = 0; var maximg = 5; …… } |
這樣會更好點,也更好理解。
添加回答
舉報
0/150
提交
取消