简体   繁体   English

在某些按钮上调用函数onclick而不影响按钮的单独onclick函数(已为按钮全部分配)-JavaScript

[英]call a function onclick on some buttons without effecting the buttons individual onclick function (all ready assigned for button) - JavaScript

I am little bit new to JavaScript and still learning. 我对JavaScript有点陌生,现在仍在学习。 I developed a dev with several buttons that have several function on onclick event. 我开发了一个带有几个按钮的开发人员,这些按钮在onclick事件上具有多个功能。 but I want to call another function when clicking any of these buttons. 但是我想在单击这些按钮中的任何一个时调用另一个函数。 without affecting the normal functions of the button. 而不影响按钮的正常功能。 (These common function change the parent div's height according to the individual function of the button). (这些通用功能根据按钮的单个功能更改父div的高度)。

I can simply put my common function inside each individual functions. 我可以简单地将我的通用功能放在每个单独的功能中。 But I want to know is there any other deffecrent way to call another function when click on all the buttons inside the particular div without manually putting the function to each buttons onclick event. 但是我想知道,在单击特定div内的所有按钮时,是否有其他方法可以调用另一个函数,而无需手动将函数放置到每个按钮的onclick事件上。 Or at least each there a way to add event listener to all the child buttons of the div at once. 或至少每个方法都有一次将事件侦听器添加到div的所有子按钮的方法。

Only using pure JavaScript 仅使用纯JavaScript

If you just add another click handler it should run multiple functions. 如果仅添加另一个单击处理程序,则它应运行多个功能。 Keep in mind you don't really have control over what order the event handlers are called. 请记住,您实际上无法控制事件处理程序的调用顺序。 So be careful with that. 所以要小心。

document.getElementsByClassName("buttonClass").addEventListener("click", myFunctionAction)

Keep in mind how you get the elements is up to you. 请记住,如何获取元素取决于您自己。 The important part is the addEventListener . 重要的部分是addEventListener Doing that multiple times for an element will run more functions on click of the button. 单击一个元素多次,将运行更多功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM