2010-08-03 65 views
1

我是JQuery的新手
我正在使用JQuery編寫簡單的數據驗證。
JQuery bind()插件如何幫助我

下面是HTML部分:

<input type="text" id="testing"> 

這是JQuery的部分:

$(document).ready(function(){ 
    $("#testing").click(function(){ 
    // doing something; 
     }); 

$("#testing").change(function(){ 
     // doing something; 
     }); 

$("#testing").mouseover(function(){ 
     // doing something; 
     }); 
}); 

$在我的示例代碼重複三次( 「#測試」),是有可能簡化這個?
我不確定bind()是否可以幫助解決這個問題。

回答

4

,你可以做以下

$(document).ready(function() { 
    var testing = $('#testing'); 
    testing.click(function() { }); 
    testing.change(function() { }); 
    testing.mouseover(function() { }); 
}); 

或(使用鏈):

$(document).ready(function() { 
    /* var testing = */ $('#testing') // the assignment is optional, but you can reuse the selector 
     .click(function() { }) 
     .change(function() { }) 
     .mouseover(function() { }); 
}); 

bind您可以創建如。自定義事件處理:

$(document).ready(function() { 
    var testing = $('#testing'); 
    testing.bind('fill', function() { }); 
    testing.trigger('fill'); 
}); 

通過的實況看書,你也可以這樣做:

$(document).ready(function() { 
    /* var testing = */ $('#testing') // the assignment is optional, but you can reuse the selector 
     .bind('click change mouseover', function() { }); 
}); 

或使用jQuery 1.4:

$(document).ready(function() { 
    /* var testing = */ $('#testing') // the assignment is optional, but you can reuse the selector 
     .bind({ 
       click: function() { }, 
       change: function() { }, 
       mouseover: function() { } 
     }); 
}); 
+0

+1爲自定義事件。 – Kobi 2010-08-03 05:40:47

2

jQuery是可鏈式的。你也可以像這樣做,

$(document).ready(function(){ 
    $("#testing").click(function(){ 
    // doing somthing; 
     }).change(function(){ 
     // doing somthing; 
     }).mouseover(function(){ 
     // doing somthing; 
     }); 
}); 

.bind()

$(document).ready(function(){ 
    $("#testing").bind({ 
     click : function(){ 
     // doing somthing; 
     }, 
     change : function(){ 
     // doing somthing; 
     }, 
     mouseover : function(){ 
     // doing somthing; 
     }  
    }); 
}); 

,如果你在所有的,只是一個動作,你可以做到這一點,

$("#testing").bind('click change mouseover', function(){ 
    // doing same thing on three events 
}) 
1

你可以在外部函數(標識具有該id的節點)的開始處僅設置var t = $("#testing");,然後調用t.clickt.changet.mouseover

1

#Testing不宜反覆三倍#是一個ID。你可以使用.Testing作爲句號表示一個班級。

<input type="text" id="Testing" class="myTesting"/> 
'.myTesting' 

我其實更喜歡我的代碼看起來像你的,因爲它更容易做好準備。鏈接有時看起來和感覺複雜,如果你錯過了或),他們可能很難追查。

但這純粹是我的看法。

+0

他不重複元素,只是查詢。 這是確定用一個ID,但鏈接的方法將有更好的表現:。 $( 「#測試」)點擊(函數(){ /*事*/ })變化(函數(){ /* something */ })。mouseover(function(){ /* something */ }); – 2010-08-03 05:25:15

+0

不確定實際的表現。儘管如此,如果性能影響最小,我仍然更喜歡可讀性, – griegs 2010-08-03 05:32:25

+0

在正常頁面中,性能影響最小,但有時頁面有數百甚至數千個元素,每個查詢都需要時間。 – 2010-08-03 05:37:00

1

鏈接的方法將有更好的表現:

$("#testing").click(function(){ 
       /* something */ 
       }).change(function(){ 
       /* something */ 
       }).mouseover(function(){ 
       /* something */ 
       }); 

你可以把任何你想要的換行符:

$("#testing").click(function(){ 
       /* something */ 
       }) // don't put a semicolon here 

      .change(function(){ 
       /* something */ 
       }) // don't put a semicolon here either 

      .mouseover(function(){ 
       /* something */ 
       }); // put it here because you will not continue chaining. 

綁定提供了額外的功能,但它不是你所要求的在這種情況下。