// Event Delegate
Function f2 (d ){
Document. onclick = function (e ){
Var e = e | window. event;
Var target = e. srcElement | e.tar get;
If (target. tagName = "LI "){
......
}
}
}
Event delegation: bind a processing function to the dom object that triggers the event at the top layer using the event bubbling mechanism. You can use event delegation when many dom objects are required to bind events.
E. srcElement | e.tar get: gets the first dom element when an event occurs.
Target. tagName: The Tag Name of the original dom element, for example, LI;
Different processing functions are executed by determining element attributes, such as tag names, IDs, and classes.
This is much more efficient than binding an event to each element and then binding a function to each event.
In the preceding example, clicking any LI will trigger the onclick event of the document. (Note that LI is not bound to any onclick event ".
Hand over the event to document. Let it process the statement that should be executed after the li click.
From Captain op