博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery和zepto的区别
阅读量:4320 次
发布时间:2019-06-06

本文共 2346 字,大约阅读时间需要 7 分钟。

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']")

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

(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是标准属性

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

Zepto.js: 由盒模型( box-sizing )决定

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

(1)边框三角形宽高的获取

jQuery 使用 .width() 和 .css('width') 都返回 ,高度也一样;

Zepto 使用 .width() 返回 ,使用 .css('width') 返回 0px 。
所以,jQuery 使用 .outerWidth() / .outerHeight() ;Zepto 使用 .width() / .height()

(2)offset()

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

jQuery: 返回 width 、 height

(3)隐藏元素

Zepto.js: 无法获取宽高;

jQuery: 可以获取。

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

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

6.DOM操作区别

jq代码

 

  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);  

zepto代码

  1. Zepto(function($) {    
  2.   var $list = $('<ul><li>Zepto 插入</li></ul>', {  
  3.     id: 'insert-by-zepto'  
  4.   });  
  5.   $list.appendTo($('body'));  
  6. }); 

jQuery 操作 ul 上的 id 不会被添加,Zepto 可以在 ul 上添加 id 

7.事件触发区别

jq代码

  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); 

zepto代码

  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. });  

使用 jQuery时load事件的处理函数不会执行,使用Zepto时load事件的处理函数会执行

 

转载于:https://www.cnblogs.com/watchmen/p/5075581.html

你可能感兴趣的文章
学习进度
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>