當前位置:編程學習大全網 - 源碼下載 - zepto和jquery的區別,zepto的不同使用8條小結

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

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是標準屬性

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

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

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

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

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

<div class="caret"></div> .caret { width: 0; height: 0; border-width: 0 20px 20px; border-color: transparent transparent blue; border-style: none dotted solid; }

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 方法參數說明 :

$("#some_element").animate({ opacity: 0.25, left: '50px', color: '#abcdef', rotateZ: '45deg', translate3d: '0,10px,0' }, 500, 'ease-out')<pre name="code" class="html">

$this_bd.animate({height:iTthis_bd_h+'px',complete:siblingsUp()},700); function siblingsUp(){ $this_bd.siblings('.bd').animate({height:'0px'},50); };

會發現zepto的complete會先執行,在執行animate動畫,所以 回調函數應該寫在後面,如

$('.warter').animate({margin:'150px 0 0 -80px',opacity:'1',rotate:'-45deg'},800,'ease-in-out',function(){alert(1);});

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

7.DOM 操作區別

jq代碼:

(function($) { $(function() { var $list = $('<ul><li>jQuery 插入</li></ul>', { id: 'insert-by-jquery' }); $list.appendTo($('body')); }); })(window.jQuery);

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

zepto代碼:

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

Zepto 可以在 ul 上添加 id 。

8.事件觸發區別

jq代碼:

(function($) { $(function() { $script = $('<script />', { src: 'http://cdn.amazeui.org/amazeui/1.0.1/js/amazeui.min.js', id: 'ui-jquery' }); $script.appendTo($('body')); $script.on('load', function() { console.log('jQ script loaded'); }); }); })(window.jQuery);

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

zepto代碼:

Zepto(function($) { $script = $('<script />', { src: 'http://cdn.amazeui.org/amazeui/1.0.1/js/amazeui.js', id: 'ui-zepto' }); $script.appendTo($('body')); $script.on('load', function() { console.log('zepto script loaded'); }); });

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

  • 上一篇:股票的asi指數是什麽意思?
  • 下一篇:二手交易網站哪個好壹點
  • copyright 2024編程學習大全網