JS three ways to create events (instance)

Source: Internet
Author: User

1. General definition of the way


onclick= "Sfont=prompt (' Please enter the text box in red ', ' red ', ' cue box ');
if (sfont== ' red ') {
form1.style.fontfamily= ' blackbody ';
Form1.style.color= ' Red ';
}"
/>


This is the most common way to define a JS event directly above the desired object. The associated transformation is the form of the calling method, as follows

<script>    function  Show ()        {            alert ("show");        } </script><input type= "button" Name= "Show"  onclick= "show ()"/>

The second Kind

for = "window" event= "onload" >    Alert ("welcome!" ); for = "window" event= "onunload" >    Alert ("thanks!" ); </script>

This defines what happens when the window is loaded and the window is unloaded.
If the event is for another object, only the value of the for property needs to be modified to the object name, and the event is modified to the monitored events. As follows:

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 with the specified event for the specified object. Called by 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 "> 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 very studious .&LT;/FORM&GT;&LT;FORMN ame= "Form2" method= "post" action= "><input type=" button "name=" button "value=" OK " onclick= "Sfont=prompt (' Please enter the text box in red ', ' red ', ' cue box '); if (sfont== ' red ') {form1.style.fontfamily= ' blackbody '; form1.style.color= ' Red ';} ' /><input type= "button" name= "Test" value= "test"/><script>functionte () {Alert ("Test");} Test.onclick=te;</script></form></body>

Record, consolidate knowledge, and facilitate others.

You can contact me for communication,[email protected].

2016.5.11

13:52

JS three ways to create events (instance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.