[英]Jquery : binding a function to a click event of an element already bound
There's a first binding done on the click event of an element : 在元素的click事件上完成了第一个绑定:
$("#element").on("click", function() {
//actions of function1
});
I would like to be able to bind another function to it, preserving the actions of the precedent function. 我希望能够将另一个功能绑定到它,并保留先前功能的操作。 Something like :
就像是 :
function elementClickAdd(elementId,newFunction){
//here, get the actions of function1
$("#"+elementId).on('click', function() {
//perform actions of function1
newFunction();
});
}
But I can't figure out how to make this. 但是我不知道该怎么做。
Could you help me please ? 请问你能帮帮我吗 ?
Thanks 谢谢
Edit : thanks for helping, the solution is here : https://jsfiddle.net/9bo6dvmb/1/ enjoy :) 编辑:感谢您的帮助,解决方案在这里: https : //jsfiddle.net/9bo6dvmb/1/ enjoy :)
If you creating the id dynamically, you can call the event after generating only, so write this line after appending the ID. 如果动态创建ID,则可以仅在生成ID后调用该事件,因此请在附加ID之后写此行。 Like below
像下面
function dynamic_bind() { $("#test").append("<div id='tt'>test example</div>"); $("#tt").on("click",function(){ hi(); }); } function hi() { alert("executed"); } dynamic_bind();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test">Sample Div</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.