zepto和jquery的區別,zepto的不同使用8條小結 會JS就會zepto


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 事件的處理函數 會 執行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM