"/> ">
Home > Article > Web Front-end > Detailed explanation of how javascript creates event definition examples
Common definition method
<input type="button" name="Button" value="确定" onclick="Sfont=prompt('请在文本框中输入红色','红色',' 提示框 '); if(Sfont=='红色'){ form1.style.fontFamily='黑体'; form1.style.color='red'; }" />
This is the most common definition method, directly defining the JS event on the required object . The relevant transformation is the form of calling the method, as follows
<script> function show() { alert("show"); } </script> <input type="button" name="show" onclick="show()"/>
The second one
<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 event 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 registration form , register the method to the specified event of the specified object. Called using the object name.
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>无标题文档</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很好学 </form> <formn ame="form2" method="post" action=""> <input type="button" name="Button" value="确定" onclick="Sfont=prompt('请在文本框中输入红色','红色',' 提示框 ');if(Sfont=='红色'){form1.style.fontFamily='黑体';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 is the detailed content of Detailed explanation of how javascript creates event definition examples. For more information, please follow other related articles on the PHP Chinese website!