+ -
当前位置:首页 → 问答吧 → 高手请进 jQuery 插件一系列问题 比如return this.each

高手请进 jQuery 插件一系列问题 比如return this.each

时间:2010-01-15

来源:互联网

jQuery 插件一系列问题
1.为什么许多插件在都会用到 return this.each(function(){}),这里的this从写法上应该是dom而不是jQuery对象,对吗?
2.
复制代码
  1.     var $jc = $.jcarousel;
  2.     $jc.fn.extend = $jc.extend = $.extend;

这是什么意思
3.
复制代码
  1. $jc.fn.extend({
  2.             reset: function() {
  3.             this.list.css(this.wh, '10px');
  4.             },
  5. })
 
这里的rest里面的this 为什么不是$(this)
4.那jqmodal为例
复制代码
  1. (function($) {
  2. $.fn.jqm=function(o){
  3. var p={
  4. overlay: 50,
  5. overlayClass: 'jqmOverlay',
  6. closeClass: 'jqmClose',
  7. trigger: '.jqModal',
  8. ajax: F,
  9. ajaxText: '',
  10. target: F,
  11. modal: F,
  12. toTop: F,
  13. onShow: F,
  14. onHide: F,
  15. onLoad: F
  16. };
  17. return this.each(function(){if(this._jqm)return H[this._jqm].c=$.extend({},H[this._jqm].c,o);s++;this._jqm=s;
  18. H[s]={c:$.extend(p,$.jqm.params,o),a:F,w:$(this).addClass('jqmID'+s),s:s};
  19. if(p.trigger)$(this).jqmAddTrigger(p.trigger);
  20. });};
  21. $.fn.jqmAddClose=function(e){return hs(this,e,'jqmHide');};
  22. $.fn.jqmAddTrigger=function(e){return hs(this,e,'jqmShow');};
  23. $.fn.jqmShow=function(t){return this.each(function(){$.jqm.open(this._jqm,t);});};
  24. $.fn.jqmHide=function(t){return this.each(function(){$.jqm.close(this._jqm,t)});};
  25. $.jqm = {
  26. hash:{},
  27. open:function(s,t){var h=H[s],c=h.c,cc='.'+c.closeClass,z=(parseInt(h.w.css('z-index'))),z=(z>0)?z:3000,o=$('<div></div>').css({height:'100%',width:'100%',position:'fixed',left:0,top:0,'z-index':z-1,opacity:c.overlay/100});if(h.a)return F;h.t=t;h.a=true;h.w.css('z-index',z);
  28.  if(c.modal) {if(!A[0])L('bind');A.push(s);}
  29.  else if(c.overlay > 0)h.w.jqmAddClose(o);
  30.  else o=F;
  31.  h.o=(o)?o.addClass(c.overlayClass).prependTo('body'):F;
  32.  if(ie6){$('html,body').css({height:'100%',width:'100%'});if(o){o=o.css({position:'absolute'})[0];for(var y in {Top:1,Left:1})o.style.setExpression(y.toLowerCase(),"(_=(document.documentElement.scroll"+y+" || document.body.scroll"+y+"))+'px'");}}
  33.  if(c.ajax) {var r=c.target||h.w,u=c.ajax,r=(typeof r == 'string')?$(r,h.w):$(r),u=(u.substr(0,1) == '@')?$(t).attr(u.substring(1)):u;
  34.   r.html(c.ajaxText).load(u,function(){if(c.onLoad)c.onLoad.call(this,h);if(cc)h.w.jqmAddClose($(cc,h.w));e(h);});}
  35.  else if(cc)h.w.jqmAddClose($(cc,h.w));
  36.  if(c.toTop&&h.o)h.w.before('<span id="jqmP'+h.w[0]._jqm+'"></span>').insertAfter(h.o); 
  37.  (c.onShow)?c.onShow(h):h.w.show();e(h);return F;
  38. },
  39. close:function(s){var h=H[s];if(!h.a)return F;h.a=F;
  40.  if(A[0]){A.pop();if(!A[0])L('unbind');}
  41.  if(h.c.toTop&&h.o)$('#jqmP'+h.w[0]._jqm).after(h.w).remove();
  42.  if(h.c.onHide)h.c.onHide(h);else{h.w.hide();if(h.o)h.o.remove();} return F;
  43. },
  44. params:{}};
  45. var s=0,H=$.jqm.hash,A=[],ie6=$.browser.msie&&($.browser.version == "6.0"),F=false,
  46. i=$('<iframe src="javascript:false;document.write(\'\');" class="jqm"></iframe>').css({opacity:0}),
  47. e=function(h){if(ie6)if(h.o)h.o.html('<p style="width:100%;height:100%"/>').prepend(i);else if(!$('iframe.jqm',h.w)[0])h.w.prepend(i); f(h);},
  48. f=function(h){try{$(':input:visible',h.w)[0].focus();}catch(_){}},
  49. L=function(t){$()[t]("keypress",m)[t]("keydown",m)[t]("mousedown",m);},
  50. m=function(e){var h=H[A[A.length-1]],r=(!$(e.target).parents('.jqmID'+h.s)[0]);if(r)f(h);return !r;},
  51. hs=function(w,t,c){return w.each(function(){var s=this._jqm;$(t).each(function() {
  52.  if(!this[c]){this[c]=[];$(this).click(function(){for(var i in {jqmShow:1,jqmHide:1})for(var s in this[i])if(H[this[i][s]])H[this[i][s]].w[i](this);return F;});}this[c].push(s);});});};
  53. })(jQuery);


这里的第二行$.fn.jqm=function(o){}
jqm为一个名字空间
这里
复制代码
  1. $.fn.jqmAddClose=function(e){return hs(this,e,'jqmHide');};

有是一个单独的jqmAddClose里面this有是什么用法
紧接着为什么又是
复制代码
  1. $.jqm = {}
这个用法这里的jqm是一个对象?
由于自己都不太理解的清除,还望高手赐教

作者: tantan199   发布时间: 2010-01-15

问题好多,不过我也不清楚。

作者: czcz   发布时间: 2010-03-30

return this.each(function(){}),  要看传入的什么啊~~~~
$.jqm = {}

这个用法这里的jqm是一个对象?
是个对象

太多了~~~不说了!~

作者: a3160586   发布时间: 2010-05-17

相关阅读 更多

热门下载

更多