JS复习:监测按钮点击事件触发方法(通过选择类)
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/927
JS复习:监测相同类的按钮点击事件触发方法
HTML部分:
<button class="myButton">点击我</button>
JavaScript部分:
var buttons = document.getElementsByClassName("myButton");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function() {
alert("Hello World!");
});
}
其它的5种方法:
[4] JS复习:监测按钮点击事件触发方法(通过属性选择器)
[5] JS复习:监测按钮点击事件触发方法(通过后代选择器)
论外:
This article was last edited at 2023-05-10 06:37:58