Javascript event stream and event binding _ javascript skills

Source: Internet
Author: User
Some of the points in this Article refer to Javascript advanced programming (a good book, we recommend you read it !), The addEvent function draws on the _ addListener method of YUI2.7. Here, I would like to thank the YUI cool people and pay tribute to them! Event stream

The event stream in the browser means that there may be one or more elements on the page to respond to the same event. The order in which one or more elements respond to events varies with browsers (mainly for IE and Netscape.

Dubbed Bubbling)

The solution on IE is the Dubbed Bubbling ). The basic idea of a bubble event is that an event is triggered in sequence from the most specific event target to the least specific event Target (document Object.

Example (1 ):Click to trigger a bubble event stream

Example (1) XHTML code structure:


Click to trigger a bubble event stream

In this example, I bound a click event to the span and a tags at the same time. As you can see, We click this a tag and click it once) two elements a and span are triggered at the same time. The first trigger is the click Event bound to the tag, and then the click Event of the span tag. That is, the"There may be one or more elements on the page to respond to the same eventAndEvents are triggered in the order from the most specific event target to the least specific event Target (document Object ).".

In this example, the first target we click is the tag link, which is the"The most specific event Target", And then the relative" span"Unspecified event targets". Looking at the XHTML code structure, you will find that the tag is contained in the span tag, that is, the span is the parent node of the tag. If you do not know the relationship between them clearly, let's take a look at the structure of the DOM tree below:

The sequence of event triggering is to continuously trigger click events from the most specific target along the DOM tree, just like the process where bubbles keep popping up from the bottom."Bubble"That is, the image obtained is also very good. It should be noted that, because I only bound a and span with a click event, the "pop" to the top of the span will be reached, if you also bind a click event to the p tag containing them and the document, the Bubbling Process will continue until the event triggering of the document ends.

In IE5.5, the top DOM node of bubble is document, and in IE6, html nodes are supported. In Mozllia1.0 and later versions, bubble is also supported, and it can be used to expose window objects.

Event Capturing)

Compared with IE4.0, Netscape4.0 uses the capture event solution. The trigger process of this event is exactly the opposite of the bubble-in a capture event, the event is triggered from the most inaccurate object (document Object), and then to the most precise object. It is still the previous example, but now it is triggered by a capture event (of course you need to test it in Netscape or Firefox ).

Example (2 ):Click "I trigger the capture event stream ".

Example (2) XHTML code structure:


Click "I trigger the capture event stream ".

The sequence triggered by the event is the opposite of the bubble above. I will not go into details here.

DOM event stream

This event stream is a standard specification developed by W3C. It supports two event stream modes at the same time, but only a capture event stream occurs before a bubble event stream occurs.

The most unique thing about DOM Event streams is that it supports text nodes and triggers events (not supported in IE ). But to be honest, I still cannot see what role it plays to allow the event support for text nodes.

Finally, according to our recent application in the development practice, we generally adopt the event stream Triggering Method of the bubble type. This is a relatively successful practice of IE. As for the reason, I think you can see from the above explanation that, after all, we certainly want to trigger events for elements from the one we most want to trigger first (from the most accurate. The first capture and then bubbling of DOM can only be used to describe how I feel. Because if we trigger an event twice according to the DOM event stream, we generally select only one type of event stream, and the value is expected to be triggered once, it's hard to understand what W3C thought at the beginning ?! One word-dizzy! Maybe my understanding is limited, but this is what I really feel.

Event processing function/listener Function

I don't want to introduce it too much here. We know that it is used in IE.AttachEvent ("NAME_OF_EVENT_HANDLER", fnHandler)Bind events to elements. In browsers that support DOM Event streams, useAddEventListener ("NAME_OF_EVENT_HANDLER", fnHandler, isCapture). Previously, I used FIREFOX to trigger capture event streams by setting isCapture (ture: capture; false: Bubble. There is also our traditional element. onclick or element ['on' + eventName]. This is the event binding listener supported by all browsers, and the result of my test is IE6 ~ in XP ~ 8. Firefox2.0 ~ 3.5, Safari4.0, Opera9.6.4, and Chrome2.0.180 are all triggered by a bubble event stream. I have never tested the older browser, but according to "Pro Javascript Techniques", the old browser uses event binding such as onclick to trigger a bubble event stream. If you are interested, find out the antique browsers and test them. But there are still events that do not support bubbling. Let's talk about it below.

Which events Support bubbling and which events do not?

This is quite interesting. The summary here comes from PPK's speech "Javascript Event" at YAHOO (we recommend you take a look at it, which is classic !), Summary of PPK content, basically only onload, unload, focus, blur, submit, and change events do not support bubbling."Bubble event stream is generally used". Naturally To keydown, keypress, keyup, click, dbclick, mousedown, mouseout, mouseover, mouseup, mousemove. In PPK, "Mouse and Key Events" supports bubbling, while Interface Events (HTML in Javascript advanced programming) (HTML is used to build interfaces) event .) Only capture is supported.

He said again: click is"Safest"Event, which supports both bubble and capture. Mouse events can trigger click, and Keyboard Events can also trigger click. Also, in browsers that support DOM events, focus and blur only support capturing. If you use the addEvent function provided below, when binding a focus event to an element or a blur event, bCapture must be set to true. So there is a problem here. IE does not support capturing. Isn't it possible to trigger this connection event? Oh, it's a serious problem? However, when the focus and blur events are used in IE, The focusin and focusout events of IE are actually triggered. Of course, these two events only support bubbling.

Although PPK said so, I still want to practice it, so I have handled it here, window. onfocus = function () {alert ('OK')}, lnkOne. onfocus = demoClick; interesting thing happened! In IE6, When you click my first example link, you can see that the bubble is complete and captured. In IE8, the bubble is only found, however, this only happens when you click this link. Then I tested it tentatively and clicked on other applications. Another unexpected situation happened to me, and the window was triggered first. onfocus, then the lnk is triggered. onfocus !! So I immediately tested IE6, the same! IE is also crazy, triggering the onfocus capture. Haha !!! Does IE support capturing, and IE is crazy ???!!!! Or is there any other explanation for this phenomenon ?? Doubt !?!?!! At first, I did think so, which surprised me. But I thought about it carefully, but the reason for the order of event execution gave me the illusion.

In IE6, click the link to trigger the onfocus first, and the prompt 'A' is displayed. Then, when the pop-up prompt box window is closed, the window gets the focus, it also triggers the focus event of the window. Then, the click event of tag A is triggered, and when the pop-up Prompt window is closed, the focus of the window is obtained, and then the focus of tag A is obtained. The IE8 test is correct. When the click event is triggered and the prompt window is closed, the focus of the window is not triggered. Ah! I was so happy that I thought windows's IE support was captured !! However, it is not completely fruitless. If you are as full as me, you should note that IE6 will be tossing twice, but it will only happen when you click, if you use the tab switch to obtain the focus, only the focus event of tag A is triggered.

In Firefox (the latest 3.5 I tested), do not use window. onfocus. Otherwise you will be down! Sorry, the person who used FIREFOX to read my post. After several OK times, you will be suspended !! Haha !!!

What is 'easy '?

IE gave me a "surprise" and this also gave me a surprise. Of course, I didn't notice it before, But YAHOO's engineers found it early. This keyword is determined based on context. In the function bound to an event, this should point to the current Element Node object and be an Element object. I think you should understand this:

Sample Code:



Click "I trigger the capture event stream ".

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.