结合两个事件处理程序,jQuery [英] Combining two event handlers, jQuery
本文介绍了结合两个事件处理程序,jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个对象上观察两个事件,
I have two events that I want to observe on an object,
input.blur(function(event) {
ajaxSubmit(this);
event.preventDefault();
});
form.submit(function(event) {
ajaxSubmit(this);
event.preventDefault();
});
但我觉得这不够干,我可以将两个事件绑定到我的对象输入
并执行我的函数 ajaxSubmit()
如果要么开火?
But I feel like this isn't dry enough, can I bind two events to my object input
and execute my function ajaxSubmit()
if either fire?
如果你能做的话,超酷的是什么:
What would be super cool is if you could do:
input.bind("blur", "submit", function(event) {
ajaxSubmit(this);
event.preventDefault();
});
推荐答案
You can use the on method
$("#yourinput").on("blur submit", functionname);
可以组合任意数量的事件;用空格分隔它们。
Can combine any number of events; separate them with a space.
这篇关于结合两个事件处理程序,jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文