jQuery pluggin -> Zepto; $.fn.extend is undefined

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I am new to zepto, and am using it as a jQuery replacement for the mobile part of a website.

So zepto doesn't have $.fn.extend. Fine that's cool with me, but I need my pluggin to work regardless of jquery or zepto.

What is zepto's alterative to fn.extend? How would you go about making a cross library extension? I've yet to find any documentation on this.

 $.fn.extend({      lineRedNAddClass : function(option){          $(this).css('border','red 1px solid').addClass(option);         }  }); 

can this be made to work with both from the same script?

回答1:

Zepto's extend function can be accessed via $.extend(), which is also available in the jQuery API, so we can simply extend $.fn using that.

Example:

$.extend($.fn, {     myFunc: function() {         $(this).css({             color: 'red'         });     } }); 

And here's a demo. I've loaded both libraries in to the assets, so just switch the value of $ using the top two lines. There's a consle.log included to prove that the correct library is loaded.

http://jsfiddle.net/WNTXY/



回答2:

in other words, to make some jquery plugins work with zepto, i've added these 2 lines to the end of my zepto.js:

jQuery = Zepto; $.fn.extend = function(obj) {     $.extend($.fn, obj); }; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!