1. Ordinary definition
<input type="button" name="Button" value="OK" onclick="Sfont=prompt('Please enter red','red',' prompt box' in the text box); if(Sfont=='red'){ form1.style.fontFamily='bold'; form1.style.color='red';}"/>This is the most common way of defining JS events directly on the required objects. The related deformation is the form of calling the method, as follows
<script> function show() { alert("show"); }</script><input type="button" name="show" onclick="show()"/>The second type
<script type="text/javascript" for="window" event="onload"> alert("Welcome!");</script><script type="text/javascript" for="window" event="onunload"> alert("Thanks!");</script>Here defines the operations that occur when loading and unloading windows.
If it is an event for other objects, you only need to modify the value of the for attribute to the object name, and the event is modified to the monitored event. as follows:
<script type="text/javascript" for="test" event="onclick">alert("button!");</script>The third type:
<input type="button" name="test" value="test"/><script>function te(){ alert("test");}test.onclick=te;</script>Here we use the form of registration to register the method to the specified event of the specified object. Use the object name to call.
The complete test code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Unt titled document</title></head><body><script type="text/javascript" for="window" event="onload">alert("Welcome!");</script><script type="text/javascript" for="window" event="onunload">alert("Thanks!");</script><form name="form1" method="post" action="">JS is easy to learn</form><formn ame="form2" method="post" action=""><input type="button" name="Button" value="OK" onclick="Sfont=prompt('Please enter red','Red',' prompt box ');if(Sfont=='red'){form1.style.fontFamily='bold';form1.style.color='red';}"/><input type="button" name="test" value="test"/><script>function te(){alert("test");}test.onclick=te;</script></form></body></html>The above three methods (example code) of JS creation events are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.