Event delegation and benefits _javascript techniques in JavaScript

Source: Internet
Author: User
Tags tag name

1, what is the event delegate: colloquially speaking, the event is onclick,onmouseover,onmouseout, and so is the event, commissioned, is to let others do, this event was added to some elements, but you add to others to do, Complete this event.

That is: using the principle of bubbling, the event is added to the parent, triggering the execution effect.

Benefits: 1, improve performance.

We can look at one example: Each Li needs to be triggered to change their background color.

<ul id= "ul" >
<li>aaaaaaaa</li>
<li>bbbbbbbb</li>
<LI>CCCCCCCC </li>
</ul> 
window.onload = function () {
var Oul = document.getElementById ("ul");
var aLi = oul.getelementsbytagname ("li");
for (var i=0; i<ali.length; i++) {
ali[i].onmouseover = function () {
this.style.background = "Red";
}
ali[i].onmouseout = function () {
this.style.background = "";
}
}
}

So we can do the add mouse event on Li.

But if we could have a lot of Li with a for loop, it would have a better effect on performance.

Here we can use the event delegate to achieve this effect. HTML does not change

Window.onload = function () {
var Oul = document.getElementById ("ul");
var aLi = oul.getelementsbytagname ("li");
/* Here to use the event Source: Event object, incident source, regardless of the event, as long as you operate the element is the event source.
ie:window.event.srcElement
Standard: Event.target
nodename: Find the tag name of the element/
oul.onmouseover = function (EV) {
var ev = EV | | window.event;
var target = Ev.target | | ev.srcelement;
alert (target.innerhtml);
if (target.nodeName.toLowerCase () = = "Li") {
target.style.background = "Red";
}
}
Oul.onmouseout = function (ev) {
var ev = EV | | window.event;
var target = Ev.target | | ev.srcelement;
alert (target.innerhtml);
if (target.nodeName.toLowerCase () = = "Li") {
Target.style.background = ""
;
}
}}

Benefits 2, the newly added elements will also have previous events.

We also take this example to see, but we have to do dynamic add Li. Click button to add Li dynamically

Such as:

<input type= "button" id= "btn"/> <ul id=
"ul" >
<li>aaaaaaaa</li>
<li> bbbbbbbb</li>
<li>cccccccc</li>
</ul> 

We do this without incident delegation:

Window.onload = function () {
var Oul = document.getElementById ("ul");
var aLi = oul.getelementsbytagname ("li");
var obtn = document.getElementById ("btn");
var inow = 4;
for (var i=0; i<ali.length; i++) {
ali[i].onmouseover = function () {
this.style.background = "Red";
}
ali[i].onmouseout = function () {
this.style.background = "";
}
}
Obtn.onclick = function () {
inow + +;
var oLi = document.createelement ("li");
oli.innerhtml = 1111 *inow;
Oul.appendchild (oLi);
}

In doing so we can see the click of the button on the newly added li without the mouse moving into the event to change their background color.

Because the FOR loop has been executed when you click Add.

Then we do it by the way of event delegation. Is that the HTML doesn't change.

Window.onload = function () {
var Oul = document.getElementById ("ul");
var aLi = oul.getelementsbytagname ("li");
var obtn = document.getElementById ("btn");
var inow = 4;
Oul.onmouseover = function (ev) {
var ev = EV | | window.event;
var target = Ev.target | | ev.srcelement;
alert (target.innerhtml);
if (target.nodeName.toLowerCase () = = "Li") {
target.style.background = "Red";
}
}
Oul.onmouseout = function (ev) {
var ev = EV | | window.event;
var target = Ev.target | | ev.srcelement;
alert (target.innerhtml);
if (target.nodeName.toLowerCase () = = "Li") {
target.style.background = "";
}
}
Obtn.onclick = function () {
inow + +;
var oLi = document.createelement ("li");
oli.innerhtml = 1111 *inow;
Oul.appendchild (oLi);
}

Ok:

As in our microblog, new tweets have the same mouse events as before.

The above is a small set of JavaScript in the description of the event entrusted and the benefits, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.