- 第一种
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> function test(){ console.log("Hello World") } </script> </head> <body> <input type="button" value="请点击" onclick="test()" /> </body> </html>
- 第二种
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="button" value="请点击" id="button" /> <script> document.getElementById("button").onclick=function(){ console.log("Hello World"); } </script> </body> </html>
- 第三种
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <input type="button" value="请点击" id="button" /> <script> document.getElementById("button").addEventListener("click",function(){ console.log("Hello World"); }); </script> </body> </html>