2010-08-03 59 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. 

绑定提供了额外的功能,但它不是你所要求的在这种情况下。