Three methods for JS event creation (instance code) and three methods for js

Source: Internet
Author: User

Three methods for JS event creation (instance code) and three methods for js

1. common definition method

<Input type = "button" name = "Button" value = "OK" onclick = "Sfont = prompt ('Enter the red', 'red' in the text box ', 'prompt box '); if (Sfont = 'red') {form1.style. fontFamily = 'simhei '; form1.style. color = 'red';} "/>

This is the most common way to define JS events directly on the desired object. 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()"/>

Second

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

The operations that occur when loading and uninstalling windows are defined here.

For events targeting other objects, you only need to change the for attribute value to the object name and the event to the monitored event. As follows:

<script type="text/javascript" for="test" event="onclick">alert("button!");</script>

Third:

<input type="button" name="test" value="test"/><script>function te(){  alert("test");}test.onclick=te;</script>

Here we use the registration form to register the method to the specified event of the specified object. Use the object name for calling.

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"> 

The above three methods for creating JS events (instance Code) are all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.

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.