JS three ways to create events (instance code) _ Basics

Source: Internet
Author: User

1. The normal way of defining

<input type= "button" name= button "value=" determines " 
onclick=" sfont=prompt (' Please enter red ', ' red ', ' hint box ') in the text box;
if (sfont== ' red ') {
form1.style.fontfamily= ' blackbody ';
Form1.style.color= ' red ';
}
/>

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

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

Second Kind

<script type= "text/javascript" for= "window" event= "onload" >
  alert ("welcome!");
</script>
<script type= "text/javascript" for= "window" event= "onunload" >
  alert ("thanks!");
</script>

This defines the actions that occur when loading and unloading windows.

If it is an event for another object, just modify the value of the for property to the object name, and the event is modified to the monitored events. 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 with the specified event for 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 ">  

Above this JS create event three methods (instance code) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.