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

全國(guó)咨詢/投訴熱線:400-618-4000

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

更新時(shí)間:2016年10月13日10時(shí)39分 來(lái)源:傳智播客前端與移動(dòng)開發(fā)學(xué)院 瀏覽次數(shù):

1. Zepto 對(duì)象 不能自定義事件

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

  結(jié)果: TypeError: Object has no method 'addEventListener'

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

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

web前端移動(dòng)開發(fā)

2. Zepto 的選擇器表達(dá)式: [name=value] 中value 必須用 雙引號(hào) " or 單引號(hào) ' 括起來(lái)

  例如執(zhí)行:$('[data-userid=123123123]')

  結(jié)果:Error: SyntaxError: DOM Exception 12

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

  2-1.zepto的選擇器沒(méi)有辦法選出 $("div[name!='abc']") 的元素

  2-2.zepto獲取select元素的選中option不能用類似jq的方法$('option[selected]'),因?yàn)閟elected屬性不是css的標(biāo)準(zhǔn)屬性

  應(yīng)該使用$('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)") 因?yàn)閐isabled是標(biāo)準(zhǔn)屬性


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


3.Zepto 是根據(jù)標(biāo)準(zhǔn)瀏覽器寫的,所以對(duì)于節(jié)點(diǎn)尺寸的方法只提供 width() 和 height(),省去了 innerWidth(), innerHeight(),outerWidth(),outerHeight()

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

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

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

  假設(shè)用下面的 HTML 和 CSS 畫了一個(gè)小三角形:


  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 。

  所以,這種場(chǎng)景,jQuery 使用 .outerWidth() / .outerHeight() ;Zepto 使用 .width() / .height() 。

  3-2.offset()

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

  jQuery: 返回 width 、 height

  3-3.隱藏元素

  Zepto.js: 無(wú)法獲取寬高;

  jQuery: 可以獲取。


4.Zepto 的each 方法只能遍歷 數(shù)組,不能遍歷JSON對(duì)象

現(xiàn)官網(wǎng)上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.
可以便利數(shù)組和鍵值對(duì)了

5.Zepto 的animate 方法參數(shù)說(shuō)明 :詳情點(diǎn)擊->

  zepto中animate的用法

6.zepto的jsonp callback函數(shù)名無(wú)法自定義

  

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 不會(huì)被添加。

  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 時(shí) load 事件的處理函數(shù) 不會(huì) 執(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 時(shí) load 事件的處理函數(shù)會(huì)執(zhí)行。




本文版權(quán)歸傳智播客web前端開發(fā)培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明作者出處,謝謝!
作者:傳智播客web前端開發(fā)培訓(xùn)學(xué)院
首發(fā):http://oisangadgets.com/web/
0 分享到:
和我們?cè)诰€交談!