May 27, 2016 morning (wonderful classroom JS Basics-3 Note one (event))

Source: Internet
Author: User
Tags tagname

first, event events1. What is an event object?   

      Used to get the details of the event: mouse position, keyboard keys

--Example: Get mouse Position: ClientX

The essence of--document: Document.childnodes[0].tagname

(1) First for the Click event, if we want to open the page, click anywhere on the page will pop up a box, then what should we do?

<script type= "Text/javascript" >        window.onload=function  () {            document.  Body. onclick=function  () {                alert ("a");}            ;        }     style= "border:1px solid red;" ></body>

When this is the code above, we found that the body is shrunk to a small bar at the top of the page only click on the top of the time will be pop-up box, (under normal circumstances the body does not stand up) then we do, where is the problem? Now let's look at:

  <script type= "Text/javascript" >        window.onload=function  () {             Document.onclick=function   () {                alert ("a");            };        }     </script>

 Now we are directly to the document add event, then can be achieved no matter where the click will have a reaction, then the question is now, what is the document exactly? So let's explore it now:

A, document is the Web page; (Yes, right)

B, document is the node; reference (Childnode) returns the child nodes of an element;

<script type= "Text/javascript" >        Alert (document.childnodes[1].tagname); HTML    </script>

  Displayed on the video is childnodes[0] "! ", and in Childnodes[1], the HTML is displayed, so the document can be called the topmost virtual parent node.

2. Event object (get mouse position)

(1) Here's how an event object is written: (click on the page and execute a function;)

<script type= "Text/javascript" >        document.onclick=function  () {            alert (  Event.clientx);        }     </script>

 In fact, the above event is an object of events, which contains the various information of the event, of which Clientx is one;

<script type= "Text/javascript" >        document.onclick=function  () {            alert (     Event.clientx+ "," +event.clienty); Do not pop out under Firefox, there is an error!        }     </script>

 The event handler function in Firefox requires the addition of parameters;

<script type= "Text/javascript" >        document.onclick=function   (EV) {            Alert (ev.clientx+ "," +ev.clienty); EV is undefined under IE, but under Firefox there is a mouse event object;        }    </script>

 And this code under IE does not show out: therefore need to deal with compatibility;

<script type= "Text/javascript" >        document.onclick=function  (EV) {              if (EV) {                alert (ev.clientx+ "," +ev.clienty ");            }            {                alert (event.clientx+ "," + Event.clienty);}}                   </script>

 But this on my computer on the IE is still not show out? What's the reason?

  Note: Because this time object is used too many times, if we use it every time to write so that it is too troublesome, then how to do? Next we continue to look at:

   Get the Event object (compatibility notation):

--var oevent=ev| | Event

  (2) or

<script type= "Text/javascript" >        vara=12| | ' ABC ';              alert (a);     </script>

 In this way, the mouse click on the display location of the event can be used to simplify the code.

<script type= "Text/javascript" >        document.onclick=function  (EV) {            var oevent=ev| |  event;                Alert (Oevent.clientx+ "," +oevent.clienty);            }     </script>
3. Event Flow

     (1) Event bubbling

Cancel bubbling: Oevent.cancelbubble=true

Example: Imitation Select control

Dom Event Flow

  

  

    

  

May 27, 2016 morning (wonderful classroom JS Basics-3 Note one (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.