zepto和jquery的区别小结

1. Zepto 对象 不能自定义事件

  例如执行: $({}).bind('cust', function(){});
 结果:  TypeError: Object has no method 'addEventListener'
  解决办法是创建一个脱离文档流的节点作为事件对象:

  例如: $('').bind('cust', function(){});


2. Zepto 的选择器表达式: [name=value]  中value 必须用 双引号 "  or 单引号 ' 括起来

  例如执行:$('[data-userid=123123123]')
         结果:Error: SyntaxError: DOM Exception 12

  解决办法: $('[data-userid="123123123]"') or $("[data-userid='123123123']")


2-1.zepto的选择器没有办法选出 $("div[name!='abc']") 的元素




2-2.zepto获取select元素的选中option不能用类似jq的方法$('option[selected]'),因为selected属性不是css的标准属性

    应该使用$('option').not(function(){ return !this.selected })
    比如:jq:$this.find('option[selected]').attr('data-v') * 1
 zepto:$this.find('option').not(function() {return !this.selected}).attr('data-v') * 1
   但是获取有select中含有disabled属性的元素可以用 $this.find("option:not(:disabled)") 因为disabled是标准属性

   参考网址:https://github.com/madrobby/zepto/issues/503


2-3、zepto在操作dom的selected和checked属性时尽量使用prop方法,以下是官方说明:




3.Zepto 是根据标准浏览器写的,所以对于节点尺寸的方法只提供 width() 和 height(),省去了 innerWidth(), innerHeight(),outerWidth(),outerHeight()

Zepto.js: 由盒模型( box-sizing )决定
jQery: 忽略盒模型,始终返回内容区域的宽/高(不包含 padding 、 border )解决方式就是使用 .css('width') 而不是 .width() 。


3-1.边框三角形宽高的获取

假设用下面的 HTML 和 CSS 画了一个小三角形:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <div class="caret"></div>   
  2. .caret {  
  3.   width: 0;  
  4.   height: 0;  
  5.   border-width: 0 20px 20px;  
  6.   border-color: transparent transparent blue;   
  7.   border-style: none dotted solid;  
  8. }  
jQuery 使用 .width() 和 .css('width') 都返回 ,高度也一样;
Zepto 使用 .width() 返回 ,使用 .css('width') 返回 0px 。
所以,这种场景,jQuery 使用 .outerWidth() / .outerHeight() ;Zepto 使用 .width() / .height() 。

3-2.offset()

Zepto.js: 返回 top 、 left 、 width 、 height
jQuery: 返回 width 、 height


3-3.隐藏元素

Zepto.js: 无法获取宽高;
jQuery: 可以获取。


4.Zepto 的each 方法只能遍历 数组,不能遍历JSON对象


5.Zepto 的animate 方法参数说明 :详情点击-> 

zepto中animate的用法


6.zepto的jsonp callback函数名无法自定义


7.DOM 操作区别

jq代码:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. (function($) {  
  2.   $(function() {  
  3.     var $list = $('<ul><li>jQuery 插入</li></ul>', {  
  4.       id: 'insert-by-jquery'  
  5.     });  
  6.     $list.appendTo($('body'));  
  7.   });  
  8. })(window.jQuery);  
jQuery 操作 ul 上的 id 不会被添加。

zepto代码:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. Zepto(function($) {    
  2.   var $list = $('<ul><li>Zepto 插入</li></ul>', {  
  3.     id: 'insert-by-zepto'  
  4.   });  
  5.   $list.appendTo($('body'));  
  6. });  
Zepto 可以在 ul 上添加 id 。


8.事件触发区别

jq代码:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. (function($) {  
  2.   $(function() {      
  3.     $script = $('<script />', {  
  4.       src: 'http://cdn.amazeui.org/amazeui/1.0.1/js/amazeui.min.js',  
  5.       id: 'ui-jquery'  
  6.     });  
  7.   
  8.     $script.appendTo($('body'));  
  9.   
  10.     $script.on('load', function() {  
  11.       console.log('jQ script loaded');  
  12.     });  
  13.   });  
  14. })(window.jQuery);  
使用 jQuery 时 load 事件的处理函数 不会 执行

zepto代码:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. Zepto(function($) {    
  2.   $script = $('<script />', {  
  3.     src: 'http://cdn.amazeui.org/amazeui/1.0.1/js/amazeui.js',  
  4.     id: 'ui-zepto'  
  5.   });  
  6.   
  7.   $script.appendTo($('body'));  
  8.   
  9.   $script.on('load', function() {  
  10.     console.log('zepto script loaded');  
  11.   });  
  12. });  
使用 Zepto 时 load 事件的处理函数 会 执行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老虎帅呆了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值