可以使用JavaScript编写此类代码,例如:
HTML代码:
JavaScript代码:
const buttons = document.querySelectorAll('button');
buttons.forEach(button => {
button.addEventListener('click', () => {
// Code for the function
});
});
在这个示例中,我们选择所有的button元素并为每个按钮添加一个事件侦听器。通过使用CSS类来区分两个按钮,我们可以执行不同的代码,例如:
const button1 = document.querySelector('.button1');
const button2 = document.querySelector('.button2');
button1.addEventListener('click', () => {
// Code for the function for Button 1
});
button2.addEventListener('click', () => {
// Code for the function for Button 2
});
上一篇:编写具有NA值的多个变量的代码
下一篇:编写具有原子递增函数的写作