JS in the event:

Source: Internet
Author: User


1. Event: JS captures the user's actions or some behavior in the page

2. Event Source: The object that triggered the event

Mouse events:








Page event, Undo load:


Add, delete event has compatibility:

In IE:

Add to:

Obj.attachevent ("On event", "handler")


Obj.detachevent ("On Event", "Method")

Google and other browsers that support the standard:

Add to:

Obj.addeventlistener ("click", Fn1,false);

Obj.removeeventlistener ("click", Fn1,false);

Custom objects:

var person={

Name: "Zhang"



Call: Console.log (Person.name)


    varEvent ={add:function (obj, type, foo) {if(obj.attachevent) {obj.attachevent (" on"+type, foo); } Else if(Obj.addeventlistener) {Obj.addeventlistener (type, foo,false); } Else{obj[" on"+ Type] =foo; }}, Remove:function (obj, type, foo) {if(obj.detachevent) {obj.detachevent (" on"+type, foo); } Else if(Obj.removeeventlistener) {Obj.removeeventlistener (type, foo,false); } Else{obj[" on"+ Type] =NULL; }            }       };

Event object:

Bu1.onclick=function (e) {

var ev=e| | Window.event


Location of mouse relative to browser:

Ev. ClientX (x-axis)

Ev. ClientY (Y-axis)

Mouse position relative to the screen:

Ev. ScreenX

Ev. ScreenY

The location of the mouse relative to the event source:



Keyboard events:

Document.onkeydown=function (e) {



div2.onmousedown=function (e) {varev=e| | Window.Event;//When an event occurs: Gets the distance to the source of the event            varOl=ev.offsety; varAl=Ev.offsetx;  This. onmousemove=function (e) {varev=e| | Window.Event;  This. style.top=ev.clienty-ol+"px";  This. style.left=ev.clientx-al+"px";                };  This. onmouseup=function () { This. onmousemove=NULL; }        };

Event Flow:

Bubbling event: Responding to an event from a point-of-click

Capture-type events: Responding to events from within an outward

Block Event Flow:


Other browsers: Ev.stoppropagation


 div3.onclick=function (e) { var  ev= e| | Window. event            I'm the son.   "  //  block bubbling event  ev.stoppropagation (); 
Body.addeventlistener ("click", Function (e) {//        var ev = e | | window.event; //         Console.log ("I Am Body"); //        //         block capture Events //        ev.stoppropagation (); //     },true);

Event delegate:

var obj=ev.target| | Ev.srcelement (returns the clicked object)

var ul=document.getelementsbytagname ("ul");    ul[0].onclick=function (e) {        var ev=e| | Window. Event ;         var obj=ev.target| | ev.srcelement;        alert (obj.innerhtml);    }

JS in the event:

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.