JavaScript Learning Notes JS Event Object _ Basics

Source: Internet
Author: User

Event object: When an event occurs, the browser automatically establishes the object and includes the type of the event, mouse coordinates, and so on.

Properties of the Event object: Format: Event. property.

Some notes:

An event represents the state of an incident, such as the element that triggers the events object, the position and state of the mouse, the key pressed, and so on;

The event object is valid only if it occurs.

Firefox in the event and IE in the different, ie in the global variable, ready to use, Firefox in the parameters to boot to use, is the run-time temporary variables.

In the Ie/opera is the window.event, in Firefox is an event;

And the object of the event, in IE is window.event.srcElement, in Firefox is Event.target,opera both are available.

Binding events

There are usually two ways to bind an event to an object (control) in JS:

First, define a function in the head:

Copy Code code as follows:

<script type= "Text/javascript" >
function ClickHandler ()
{
Do something
Alert ("button is clicked!");
}
</script>

The first method of binding events:

<input type= "button" value= "Button1" onclick= "ClickHandler ();" ><br/>
The second method of binding events:

Copy Code code as follows:

<input type= "button" id= "Button2" value= "Button2" >
<script type= "Text/javascript" >
var v = document.getElementById ("Button2");
V.onclick = ClickHandler; Function name here, not parentheses
</script>

Other instances

Example 1:

Copy Code code as follows:

<! DOCTYPE html>
<title>eventTest.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<script>
function MOver (object) {
Object.color = "Red";
}
function Mout (object) {
Object.color = "Blue";
}
</script>
<body>
<font style= "Cursor:help"
onclick= "window.location.href= ' http://www.baidu.com '"
Onmouseover= "MOver (This)" onmouseout= "Mout (This)" > Welcome visit </font>
</body>

Example 2:

Copy Code code as follows:

<! DOCTYPE html>
<title>eventTest2.html</title>
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >-->
<body>
<script type= "Text/javascript" >
function GetEvent (event) {
Alert ("Event Type:" + event.type);
}
document.write ("click ...");
Document.onmousedown = getEvent;
</script>
</body>

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.