简体   繁体   English

如果在javascript中更改按钮单击按钮时如何运行某种方法?

[英]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.

相关问题 在页面加载和每次单击某些按钮时运行jQuery函数 - Run jQuery Function on Page Load AND Every Time Certain Buttons Are Clicked 为什么每次单击按钮都会成倍增加? (javascript ejs) - Why are buttons being multiplied every time one is clicked? (javascript ejs) 每两次单击按钮运行一次JavaScript函数 - Run JavaScript function for every two times a button is clicked 每次使用 javascript 和 jquery 单击按钮时更改文本 - change text every time button clicked with javascript and jquery 每次在JavaScript中另一个变量的值单击按钮时,如何使变量减小? - How do you make a Variable decrease every time a button is clicked by the value of another Variable in JavaScript? 如果只单击一个,如何避免更改所有按钮的颜色? JavaScript - How to avoid changing colors on all buttons if only one is clicked? JavaScript 每次单击某个特定对象时如何使其他对象出现 - How to make other objects appear every time a certain object in clicked 如果使用JavaScript动态添加了按钮,如何知道单击了哪个按钮? - How to know that which button is clicked, if buttons are added dynamically using javascript? 每次单击跨度时,如何为 div 运行 function? - How to run a function for a div every time the span is clicked? 如何在每次单击按钮时将字符串设置为新的格? - how to set a string to a new division every single time a button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM