Day Fourth: Event object (common two properties and two methods)

Source: Internet
Author: User

Due to holiday, the information can not be updated in time, sorry

Learning content:

Event object:

When a DOM event is triggered, an object is generated

Common Properties of events

Type: Get event types

Target: Gets the event target

The code is as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Event Objects </title>
<body>
<button id= "btn" onclick= "" > Buttons </button>
</body>
<script>
document.getElementById ("Btn"). AddEventListener ("click", ShowType);
function ShowType (event) {
alert (event.type); Gets the type of the event "click", if modified to MouseOver can also
alert (event.target); Get the target of the event
}

</script>

Additional notes:
When you get the event type, the dialog box pops up and the dialog box displays the click
To get the event target, click the button and the popup box is as follows:

Common methods of events

Stoppropagation (): Block event bubbling

Preventdefault (): Block event default behavior

An example of what is event bubbling

The code is as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Event Objects </title>
<body>
<div id= "div" >
<button id= "btn" onclick= "" > Buttons </button>
</div>
</body>
<script>
document.getElementById ("Btn"). AddEventListener ("click", ShowType);
document.getElementById ("div"). AddEventListener ("click", Showdiv);
function ShowType (event) {

alert (event.target);
}
function Showdiv (event) {
Alert ("div");
}
</script>

Operation Result:

Click the button, pop up the dialog box, return the event target, close the dialog box, and then pop up the dialog box content "Div"

Although just click on the button, but the event is passed up, because the button belongs to the Div, this is the event bubbling, how to prevent the bubbling of events, add the following code

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Event Objects </title>
<body>
<div id= "div" >
<button id= "btn" onclick= "" > Buttons </button>
</div>
</body>
<script>
document.getElementById ("Btn"). AddEventListener ("click", ShowType);
document.getElementById ("div"). AddEventListener ("click", Showdiv);
function ShowType (event) {

alert (event.target);
Event.stoppropagation (); Block event bubbling
}
function Showdiv (event) {
Alert ("div");
}
</script>

Block event bubbling, the first box pops up and does not eject the contents of the Showdiv function


Default behavior for blocking events

What is the default behavior of the event, such as we add a connection, the connection text "Baidu" will default to Baidu page

Example: <a href= "http://www.baidu.com" > Baidu </a>

The code is as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> Event Objects </title>
<body>
<div id= "div" >
<button id= "btn" onclick= "" > Buttons </button>
<a href= "http://www.baidu.com" id= "aid" > Baidu </a>
</div>
</body>
<script>
document.getElementById ("Btn"). AddEventListener ("click", ShowType);
document.getElementById ("div"). AddEventListener ("click", Showdiv);
document.getElementById ("Aid"). AddEventListener ("click", ShowA);
function ShowType (event) {
alert (event.target);
Event.stoppropagation ();
}
function Showdiv (event) {
Alert ("div");
}
function ShowA (event) {
Event.stoppropagation (); Stop the bubbling behavior, or jump out of the div first
Event.preventdefault (); Block default behavior, turn on Baidu
}
</script>

Day Fourth: Event object (common two properties and two methods)

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.