JQuery pluggin & # 8594; Zepto; $ .fn.extend - undefined

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

So zepto does not have $ .fn.extend. Great, which is cool with me, but I need my pluggin to work regardless of jquery or zepto.

What is zecto alterative for fn.extend? How could you make a cross-library extension? I have not yet found any documentation on this.

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

Can this be done to work as with the same script?

+4
source share
2 answers

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

Example:

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

And here is the demo. I loaded both libraries into assets, so just switch the $ value with the top two lines. There consle.log included to prove that the correct library is loaded.

http://jsfiddle.net/WNTXY/

+3
source

In other words, for some jquery plugins to work with zepto, I added these two lines to the end of my zepto.js:

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

Source: https://habr.com/ru/post/1443842/


All Articles