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
提交
取消