jQuery 常見面試題

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   java   ar   strong   

一 :Q: What is the difference between .get(), [], and .eq()?

A: eq返回原生jQuery對象,截取某些el元素產生Jquery新對象

 get和[]返回的都是原生的Dom對象,原理一致

 get和[]區別是get是通過jQuery對象的方法擷取,[]是根據jQuery是一個數組對象擷取

 

二: What is the difference between .bind(), .live(), and .delegate()?

A: 它們實質調用的都是jQuery執行個體對象的on函數,更底層是jQuery.event.add();

官方描述:Attach an event handler function for one or more events to the selected elements

.on( events [, selector ] [, data ], handler(eventObject) )

三種綁定函數代碼

 bind: this.on(types, null, data, fn); 直接綁定到元素上

live: jQuery(this.context).on(types, this.selector, data, fn); 將事件綁定到context上,冒泡,當觸發元素為this.selector時觸發

delegate: this.on(types. selector, data, fn)

selector如何添加三: How, and why, would you namespace a bound event handler?

A: click.myPlugin.simple定義了兩個命名空間 為這個獨特的click事件 可以被移除通過 .off(‘click.myPlugin‘) or .off(‘click.simple‘)

命名空間跟css 相似都不是分層的,只需要一個名字來匹配

jquery.event jquery.event.global jquery.event.handle

四:What is the difference between $ and $.fn? Or just what is $.fn.?
123 window.jQuery = window.$ = jQuery;  jQuery.fn = jQuery.prototype = {}
五:what‘s jQuery‘s context/selector and why use it ?
123 <div id="context">     <div id="inner"></div> </div>

  

context/selector 樣本

?
123456789 <script> var $ret = $(‘#inner‘, $(‘#context‘)[0]); console.log($ret.selector); // #inner console.log($ret.context); // #context   var $ret = $(‘#inner‘, ‘#context‘); console.log( $ret.selector); // ‘#context #inner‘ console.log( $ret.context); // document </script>

context 就是限定尋找的範圍

context 必須是一個DOM元素,context 底層還是用了.find方法來實現

官方API selector context is implemented with the .find() method,so $("span", this) is equivalent to $(this).find("span")

註:例子僅供展示,id類型尋找非常快,所以不要用這種context,直接$(‘#inner‘)最好,當尋找tag/class時用會比較高效.

 六:Difference between ‘delegate()‘ and ‘live()‘

delegate 指定了委派物件

live委託給了jQuery的context,1.9以後刪除了,用on代替

一下三個效果一致

?
123 $(selector).live(events, data, handler); $(document).delegate(selector, events. data, handler); $(document).on(events, selector, data, handler);
七:What is the effetcs (of fx) queue?

.queue([queueName])

官方API:Show the queue of functions to be executed on the matched elements.

Queues allow a sequence of actions to be called on an element asynchronously, without halting program execution. 最大的特點是這些代碼非同步執行,不影響後面代碼操作,說白了就是將他們放入一個隊列中

?
12345678910111213 div { margin:3px; width:40px; height:40px;         position:absolute; left:0px; top:60px;         background:green; display:none;  }   div.newcolor { background:blue; } p { color:red; }     <p>The queue length is: <span></span></p> <div id="box">1</div> <div style="top:120px;">2</div> <button id="start">start</button> <button id="end">end</button>

  

?
12345678910111213141516171819202122232425262728293031323334353637383940414243 <script> var $box = $(‘div‘);   function runIt() {     $box.show()         .animate({‘left‘:"+=200"}, 2000)         .queue(function(next){             $(this).addClass(‘newcolor‘);             next();         })         .slideToggle(1000)         .slideToggle(‘fast‘)         .animate({‘left‘:"-=200"}, 2000)         .queue(function(next){             $(this).removeClass(‘newcolor‘);             next();         })           .hide(‘slow‘)         .show(200)         .slideUp("normal"); }   function showIt(){     var n = $box.queue();     $("span").text(n.length);     setTimeout(showIt, 100); }   function stopIt(){     $box.queue(‘fx‘, []);     $box.stop(); }   $(‘#start‘).click(function(){     runIt(); }); $(‘#end‘).click(function(){     stopIt(); }) showIt();     </script>
八:attr和prop的區別  

attr是操作屬性節點,DOM的API setAttribute,getAttribute(HTML)

prop是操作擷取到的對應js對象的屬性 (JS)

情境:遇到要擷取或設定checked,selected,readonly和disabled等屬性時,用prop方法顯然更好

prop

jQuery 常見面試題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.