教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

web前端開發(fā)培訓之zepto和jquery的區(qū)別,zepto的不同使用小結

更新時間:2016年10月13日10時39分 來源:傳智播客前端與移動開發(fā)學院 瀏覽次數:

1. Zepto 對象 不能自定義事件

      例如執(zhí)行: $({}).bind('cust', function(){});

  結果: TypeError: Object has no method 'addEventListener'

       解決辦法是創(chuàng)建一個脫離文檔流的節(jié)點作為事件對象:

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

web前端移動開發(fā)

2. Zepto 的選擇器表達式: [name=value] 中value 必須用 雙引號 " or 單引號 ' 括起來

  例如執(zhí)行:$('[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是標準屬性


  2-3、zepto在操作dom的selected和checked屬性時盡量使用prop方法,以下是官方說明:


3.Zepto 是根據標準瀏覽器寫的,所以對于節(jié)點尺寸的方法只提供 width() 和 height(),省去了 innerWidth(), innerHeight(),outerWidth(),outerHeight()

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

  jQery: 忽略盒模型,始終返回內容區(qū)域的寬/高(不包含 padding 、 border )解決方式就是使用 .css('width') 而不是 .width() 。

  3-1.邊框三角形寬高的獲取

  假設用下面的 HTML 和 CSS 畫了一個小三角形:


  1. <font color="rgb(85, 85, 85)"><font color="rgb(85, 85, 85)"><font size="3"><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. } </font></font></font>

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對象

現官網上each的定義
$.each(collection, function(index, item){ ... }) ⇒ collection
Iterate over array elements or object key-value pairs. Returning false from the iterator function stops the iteration.
可以便利數組和鍵值對了

5.Zepto 的animate 方法參數說明 :詳情點擊->

  zepto中animate的用法

6.zepto的jsonp callback函數名無法自定義

  

7.DOM 操作區(qū)別

  jq代碼:


  1. <font color="rgb(85, 85, 85)"><font color="rgb(85, 85, 85)"><font size="3">(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); </font></font></font>


jQuery 操作 ul 上的 id 不會被添加。

  zepto代碼:


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



Zepto 可以在 ul 上添加 id 。

8.事件觸發(fā)區(qū)別

  jq代碼:


  1. <font color="rgb(85, 85, 85)"><font color="rgb(85, 85, 85)"><font size="3">(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);   </font></font></font>


使用 jQuery 時 load 事件的處理函數 不會 執(zhí)行

zepto代碼:


  1. <font color="rgb(85, 85, 85)"><font color="rgb(85, 85, 85)"><font size="3">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. }); </font></font></font>

使用 Zepto 時 load 事件的處理函數會執(zhí)行。




本文版權歸傳智播客web前端開發(fā)培訓學院所有,歡迎轉載,轉載請注明作者出處,謝謝!
作者:傳智播客web前端開發(fā)培訓學院
首發(fā):http://www.xamj520.com/web/
0 分享到:
和我們在線交談!