jQuery对不同元素的不同事件触发相同的功能 [英] jQuery different events on different elements to trigger the same function
本文介绍了jQuery对不同元素的不同事件触发相同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果不同的元素必须在不同的事件上触发相同的功能怎么办?
What if different elements have to trigger the same function but on different events ?
$('#btnNext').on('click', function() { /*Same thing*/ });
$('#txtField').on('change blur', function() { /*Same thing*/ });
有没有办法整合这两行,所以我只需编写一行相同的代码?
Is there any way to integrate these two lines, so I can write the same lines of code just once ?
推荐答案
您在问题中包含了一条线索:触发相同的功能 - 所以只需绑定相同的函数:
You included a clue in your question: "trigger the same function" - so simply bind the same function:
function commonHandler(e) { /* your code */ }
$('#btnNext').on('click', commonHandler);
$('#txtField').on('change blur', commonHandler);
这篇关于jQuery对不同元素的不同事件触发相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文