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/930
JS复习:监测按钮点击事件触发方法(通过后代选择器)
这个你也不一定常用。但是还是需要记忆。
HTML部分:
<div id="myDiv">
<button>点击我</button>
</div>
JavaScript部分:
var myDiv = document.getElementById("myDiv");
var button = myDiv.querySelector("button");
button.addEventListener("click", function() {
alert("Hello World!");
});
其它5种方法:
[5] JS复习:监测按钮点击事件触发方法(通过属性选择器)
论外:
This article was last edited at 2023-05-10 06:38:39