JavaScript learning 11: getting started with events, javascript11

Source: Internet
Author: User

JavaScript learning 11: getting started with events, javascript11

JavaScript events are a series of operations caused by users accessing Web pages, such as clicking the mouse and hitting the keyboard. When a user executes a similar operation, it triggers the execution of a series of code to respond to the user operation. This is called an event.

Events are generally used for interaction between browsers and user operations. Generally, we divide the event model in JavaScript into three types: inline model, script model, and DOM2 model.

Inline Model

Is the most traditional way to handle events. In this model, the event handler function is an attribute of the HTML Tag and is used to process specified events. Although inline is often used in the early days, it is mixed with html and is not separated from html. The following is an example of an inline model:

<Span style = "font-size: 18px;"> <inputtype = "button" value = "button" onclick = "alert ('lianjiangwei '); "/> </span>

Script Model

Because the inline model violates the principle of hierarchical separation of HTML and JavaScript code, we try to process events in JavaScript to solve this problem, thus generating the script model. Example:

<Span style = "font-size: 18px;"> var input = document. getElementsByTagName ('input') [0]; // obtain the input object. onclick = function () {// anonymous function execution alert ('lianjianwei') ;}; </span>

DOM2 Model

Among all modern browsers-All Versions earlier than IE9 have implemented the DOM2 standard event model, which stipulates that the events triggered by each DOM element must go through three phases: 1. Capture phase; 2. Event Processing Program Calling phase of the target object; 3. Bubble phase.

Bubble stage: when a certain type of event occurs on the document element, it will spread up in the document tree, that is, calling the same type of event handler function of the parent element.

Capture phase: the capture phase seems to be a reverse bubble phase. First, call the capture handler of the window object, then the capture handler of the document Object, followed by the body object, followed by the Dom tree down, and so on, wait until the capture event handler of the parent element of the event Target element is called. Registering a capture event handler on the target Element Object will not be called.

Event processing functions

The types of events that JavaScript can handle are mouse events, keyboard events, and HTML events.

For each event, it has its own trigger range and method. If the trigger range and method are exceeded, the event processing will become invalid.

For mouse events, all elements of the page can be triggered.

A keyboard event is triggered when you press a key on the keyboard, which can be divided into keydown, keypress, and keyup.

HTML events trigger corresponding events when TAG elements on pages or pages change, such as load events, when the page is fully loaded, it is triggered on the window or on the frame set after the frame set is loaded. Similarly, there are unload, select, change, and so on.

Let's take a look at several simple code examples:

<Span style = "font-size: 18px;"> // event processing function example // 1 mouse event input. onmousedown = function () {alert ('you did not put your mouse down! ');} // 2 Keyboard event onkeydown = function () {alert (' You have moved the keyboard! ');} // 3 HTML event window, onscroll = function () {alert (' You are moving the scroll bar! ') ;}</Span>

Summary: HTML is the static description of the Web page, while JavaScript adds dynamic effects to the Web page. How to make the two interact is what we are watching, of course, these are the basic concepts of events. We will break down the event objects and events.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.