[英]binding multiple events to one selector in jQuery
$("#action_button").click(function() {
如何将具有不同选择器的onEnter事件绑定到上面的相同代码?
Create a named function and use that instead of an anonymous function. 创建一个命名函数,并使用它代替匿名函数。
$("#action_button").click(processAction);
$("#otherSelector").otherEvent(processAction);
function processAction() {
// your code here
}
move the logic to a common method and call that from everywhere.for onenter event,bind keyup and check for keycode 13. 将逻辑移动到通用方法并从各处调用该方法。对于onenter事件,请绑定键并检查键码13。
$("#new_selector").keyup(function(event){
var keyCode = event.keyCode || event.which; // browser compatible
if(keyCode === 13){
do_something();
}
});
$("#action_button").click(function() {
do_something();
});
function do_something(){
//
}
did you mean add events on the same line chaining one selector? 您的意思是在同一行上将事件添加到一个选择器上吗? as far as i know you have to have a brand new statement. 据我了解,您必须有一个全新的声明。 you could try: 您可以尝试:
$("#action_button").click(function() {...}).add('other-selector').onEnter... but i dont know if that works at all. $(“#action_button”)。click(function(){...})。add('other-selector')。onEnter ...但我不知道这是否可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.