您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

js绑定单击事件,js单击事件

来源:互联网 浏览:71次 时间:2023-04-08

onclick:鼠标的单击事件

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>单机事件</title> <style> .box{ width: 200px; height: 200px; border: 1px solid; line-height: 200px; text-align: center; font-size: 30px; } </style></head><body> <button class="but">按钮1</button> <button class="but" onclick="myClick()">按钮2</button> <div class="box">1</div> <script> but = document.getElementsByClassName("but"); box = document.getElementsByClassName("box"); // 方法一获取元素直接在js中给元素绑定,不需要函数名 var s = 1; but[0].onclick = function(){ s++; box[0].innerHTML = s; } // 方法二定义好函数在html标签中绑定 直接拿来使用 function myClick(){ s--; box[0].innerHTML = s; } </script></body></html> 18103686