Event bubbling and event capture in JavaScript

Source: Internet
Author: User

Focus Event
Onfocus: Getting focus
Onblur: Losing focus

Event bubbling (and style independent, structure)
When an element receives an event, it propagates all the events he receives to his parent, always to the top window

Block bubbling:
Called in the event function that currently wants to block bubbling
Event.cancelbubble=true; Current object that blocks the current event

Event Capture:

Binding events:
1.obj.onclick=function () {}//an object with one event bound 2 functions will produce overrides
2.ie:obj.attachevent (' onclick ', fn1)
Standard: Obj.addeventlistener (' Click ', Fn1, capture)
The default is not to capture, bubbling

Problem:
The this point in IE under attachevent points to window
Call Method Fn1 () ==fn1.call () The first argument can change this point
Encapsulation of bound events:
function bind (OBJ,EVNAME,FN) {
if (Obj.addeventlistener) {
Obj.addeventlistener (Evname,fn,false)
}else{
Obj.attachevent (' on ' +evname,function () {
Fn.call (obj)
})
}
}

Go out and bubble, come in and catch
Event Capture:
Obj.addeventlistener (' click ', fn,true)

Event Cancellation:
1.obj.onclick=null; Assignment cancellation
2.ie:obj.dettachevent (OBJ,FN)
Standard: Obj.removeeventlistener (OBJ,FN, capture)

Event bubbling and event capture in JavaScript

Related Article

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.