亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有誰知道jQuery(function($){...})中的 “$” 是什么意思?

有誰知道jQuery(function($){...})中的 “$” 是什么意思?

蝴蝶不菲 2022-01-12 14:11:11
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;    ……}

這樣會更好點,也更好理解。



查看完整回答
反對 回復 2022-01-16
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

等同于:
$(document).ready(function(){
//你寫的代碼

})

查看完整回答
反對 回復 2022-01-16
  • 2 回答
  • 0 關注
  • 298 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號