Home > Article > Web Front-end > How to set element click event in javascript
Javascript method to set element click event: 1. Create an HTML sample file; 2. Add script tag; 3. Get button element through getElementById; 4. Set element click event through "btn.onclick" method. Can.
The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer
How to set element click event in javascript?
It’s very simple, look at the code below:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> window.onload=function(){ var btn = document.getElementById("btn"); btn.onclick=function(){ alert("这就是点击事件~"); } } </script> </head> <body> <button id="btn">点击一下吧</button> </body> </html>
Recommended learning: "javascript basic tutorial"
The above is the detailed content of How to set element click event in javascript. For more information, please follow other related articles on the PHP Chinese website!