


Detailed explanation of how javascript creates event definition examples
Jul 22, 2017 pm 04:13 PMCommon 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What is Discuz? Definition and function introduction of Discuz

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

Simple JavaScript Tutorial: How to Get HTTP Status Code

The definition and function of MySQL composite primary key

Introduction to PHP interfaces and how to define them

How to get HTTP status code in JavaScript the easy way
