<!DOCTYPE html> <html> <head> <script> function fun() { alert("Welcome to the lidihuo.com"); } </script> </head> <body> <h3> This is an example of using onclick attribute in HTML. </h3> <p> Click the following button to see the effect. </p> <button onclick="fun()"> Click me</button> </body> </html>
输出
This is an example of using onclick attribute in HTML.
<!DOCTYPE html> <html> <head> <title> onclick event </title> </head> <body> <h3> This is an example of using onclick event. </h3> <p> Click the following text to see the effect. </p> <p id="para"> Click me</p> <script> document.getElementById("para").onclick=function() { fun() } ; function fun() { document.getElementById("para").innerHTML="Welcome to the lidihuo.com"; document.getElementById("para").style.color="blue"; document.getElementById("para").style.backgroundColor="yellow"; document.getElementById("para").style.fontSize="25px"; document.getElementById("para").style.border="4px solid red"; } </script> </body> </html>
<!DOCTYPE html> <html> <head> </head> <body> <h3> This is an example of using click event. </h3> <p> Click the following text to see the effect. </p> <p id="para"> Click me</p> <script> document.getElementById("para").onclick=function() { fun() } ; function fun() { document.getElementById("para").innerHTML="Welcome to the lidihuo.com"; document.getElementsByTagName("body")[0].style.color="blue"; document.getElementsByTagName("body")[0].style.backgroundColor="lightgreen"; document.getElementsByTagName("body")[0].style.fontSize="25px"; document.getElementById("para").style.border="4px solid red"; } </script> </body> </html>