JS's onload event and initialization button Event sample code _javascript Tips

Source: Internet
Author: User
The common affirmation is <body onload= "" > This usage. In fact, not only can you use it on the body to support multiple HTML tags. The usage is as follows:

OnLoad event is triggered when a page or a picture is loaded complete.

Supported HTML Tags:
<body&gt, <frame>, <frameset>, <iframe>, , <link> <script>

Example:
The first of these methods
Copy Code code as follows:

<body onload= "Javascript:alert (' liuzm.com ')" ></BODY>

The second method
Copy Code code as follows:

<script>window.onload=function () {alert (' liuzm.com ')}</script>
<title>event test</title>
<body onload= "init ()" >
<!--binding method one: In the element, the binding methods are set by OnXxx (event)-->
<button id= "BTN1" onclick= "display ()" > Binding mode One </button>

<!--binding Method Two: In JavaScript code, the OnXxx (event) of the element is set to bind methods by obtaining the element-->
<button id= "BTN2" > Binding mode Two </button>


<!--bind Three: Bind event (ie4+) by for, event as element. For the following is element id,event is a specific event-->
<button id= "BTN3" > Binding mode three </button>

<!--binding event (ie5+) for elements by Attachevent. The first argument is the event name, and the second parameter is the method of the binding-->
<button id= "BTN4" > Binding mode Four </button>
</body>

<script type= "Text/javascript" >
function init () {
document.getElementById ("Btn2"). onclick = display;//for button2 binding events
document.getElementById ("Btn4"). Attachevent ("onclick", display);//For Button4 binding event
}

Instance:
Copy Code code as follows:
Function display (event) {
var targ;//an object reference that triggers an event
if (!event) {
var event = window.event;//Get Current event (IE)
}
if (event.target) {//ie has no target
Targ = Evente.target;
} else if (event.srcelement) {//applies to IE
Targ = event.srcelement;
}

//action on the object that triggers the event
Alert (targ.tagname+ "-+targ.id+"-"+event.x+"-"+event.offsetx");
targ.disabled= "Disabled";
}
</script>
<script for= "btn3" event= "onclick" >
Display ()//For Button3 bound events
</scri Pt>

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.