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

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");
}

蝴蝶不菲
浏览 307回答 2
2回答

牧羊人nacy

实际上,jquery只是js写出来的对象,或者称工厂(产生新的对象)jquery源码中的定义可以理解为 var jQuery = $ = function($){ } (jQuery)即,function参数为形参,function后的括号内的内容为实参,实参赋值给形参在你给出的例子中,可以断言肯定已经引入了jquery,故其实$已经被赋值为jQuery对象,因此这个函数是将jquery作为参数传进函数内部,作为jquery的代名词。可能你要问为什么要这么做,直接用$不是很好么?这有一种可能是不止引入了jquery,还引入了prototype(也是使用$作为工厂符号)之类的,为防混淆如此做。这样做其实是因为js编程中的封装,防止变量污染其他作用域,使得$只作用于这个函数。但我感觉这样写的不是很好,如果换成jQuery(function(jQuery){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;$&nbsp;=&nbsp;jQuery;&nbsp;&nbsp;&nbsp;&nbsp;//alert($);&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;index&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;maximg&nbsp;=&nbsp;5;&nbsp;&nbsp;&nbsp;&nbsp;……}这样会更好点,也更好理解。

POPMUISE

等同于:$(document).ready(function(){//你写的代码})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JQuery