JQuery和Prototype區別小結

 

JQuery和Prototype區別小結

jQuery使用得比較順手也比較喜歡,不得已也要用Prototype,小小整理下區別。。

頁面載入


  1. // JQuery
    
  2. $ ( document ). ready ( function () {
  3.         // Code
  4. });

 


  1. // JQuery Shorthand
    
  2. $ ( function () {
  3.         // Code
  4. });

 


  1. // Prototype
    
  2. document . observe ( 'dom:loaded' , function () {
  3.         // Code
  4. });

 

根據ID獲取


  1. // JQuery
    
  2. $ ( "#idname" );

  1. // Prototype
    
    
  2. $ ( "idname" );

 

根據類名


  1. // JQuery
    
  2. $ ( ".classname" );

  1. // Prototype
    
    
  2. $$ ( '.classname' );

 

根據第一個符合的類名


  1. // JQuery
    
  2. $ ( ".classname:first-child" );

  1. // Prototype
    
    
  2. $$ ( '.classname' )[ 0 ];

 

根據ID綁定監聽事件


  1. // JQuery 
    
  2. $ ( "#item" ). bind ( 'click' , function () {
  3.         // Code
  4. });
  5.  
  6. // JQuery Shorthand
  7. $ ( "#item" ). click ( function () {
  8.         // Code
  9. });

  1. // Prototype
    
  2. $ ( "#item" ). observe ( 'click' , function () {
  3.         // code
  4. });

 

根據符合的類名綁定監聽事件


  1. $(".classname").bind('click',function(){
    
  2.         // code
  3. });
  4.  
  5. // JQuery Shorthand
  6. $ ( ".classname" ). click ( function () {
  7.         // code
  8. });

 


  1. // Prototype
    
  2. $$ ( ".classname" ). invoke ( 'observe' , 'click' , function () {
  3.         // code
  4. });

 

結束終止事件


  1. // JQuery
    
  2. $ ( "#id" ). click ( function () {
  3.  
  4.         //code
  5.         return false ;
  6. });

  1. // Prototype
    
  2. $ ( "id" ). observe ( 'click' , function ( e ) {
  3.         //code
  4.         Event . stop ( e );
  5. });

 

處理觀察的元素


  1. // JQuery
    
  2. $ ( '#idname' ). click ( function () {
  3.         this . hide (); // Hide the item clicked
  4. });

  1. // Prototype
    
  2. $ ( 'idname' ). observe ( 'click' , function ( e ) {
  3.         el = e . findElement ;
  4.         el . hide (); // hide the item clicked
  5. });

 

根據ID操作類名


  1. // JQuery
    
  2. $ ( '#id' ). addClass ( 'red' );
  3. $ ( '#id' ). removeClass ( 'red' );

  1. // Prototype
    
  2. $ ( 'id' ). addClassName ( 'red' );
  3. $ ( 'id' ). removeClassName ( 'red' );

 

根據類名操作類名。。


  1. // JQuery
    
  2. $ ( '.class' ). addClass ( 'red' );
  3. $ ( '.class' ). removeClass ( 'red' );

  1. // Prototype
    
  2. $$ ( '.class' ). invoke ( 'addClassName' , 'red' );
  3. $$ ( '.class' ). invoke ( 'removeClassName' , 'red' );

 

AJAX請求和JSON應用


  1. $.get(url,function(data){
            alert(data . name );
    
  2. }, 'JSON' );

 


  1. // Prototype
    new   Ajax . Request ( url ,   {
  2.         method : 'get' ,
  3.         onSuccess : function ( transport , json ) {
  4.                 alert ( json . name );
  5.         }
  6. });

可以得出結論:jQuery和Prototype大部分是極爲相似的,多用幾次就都熟了。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章