可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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); };