admin 管理员组文章数量: 1086019
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 ?
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 ?
Share Improve this question asked Jun 4, 2013 at 11:38 hsukhsuk 6,87013 gold badges52 silver badges81 bronze badges3 Answers
Reset to default 9You included a clue in your question: "trigger the same function" - so simply bind the same function:
function monHandler(e) { /* your code */ }
$('#btnNext').on('click', monHandler);
$('#txtField').on('change blur', monHandler);
$('#btnNext').on('click', myMethod );
$('#txtField').on('change blur', myMethod );
function myMethod()
{
/*Your Code goes here*/
}
Here is a jsFiddle For You
Fiddle Here
function Commonalert(){alert("Common Alert Message");}
$('#Btn').on('click', Commonalert);
$('#text').on('change blur', Commonalert);
本文标签: javascriptjQuery different events on different elements to trigger the same functionStack Overflow
版权声明:本文标题:javascript - jQuery different events on different elements to trigger the same function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744076683a2529478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论