[英]How to run a certain method every time a button is clicked with changing buttons in javascript?
I want to be able to run the content() method every time a button is clicked with the id of the button as the parameter. 我希望每次单击按钮时都能运行content()方法,并将按钮的id作为参数。 This method removes the old buttons and displays new buttons.
此方法删除旧按钮并显示新按钮。
This is the code I have so far: 这是我到目前为止的代码:
do {
selection = $('button').click(function() {return this.id;});
content(selection);
} while(selection !== "end");
What you need is a click event handler where you call the content
method 您需要的是一个单击事件处理程序,您可以在其中调用
content
方法
$('button').click(function () {
content(this.id);
});
If you want to execute the same method again when the replaced button is clicked then you need to use event delegation 如果要在单击替换按钮时再次执行相同的方法,则需要使用事件委派
$(document).on('click', 'button', function () {
content(this.id);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.