Event
1 | var EventUtil = { |
Event delegation
1 | < ul id="myLinks"> |
1 | var item1 = document.getElementById("goSomewhere"); |
Could be changed to1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19var list = document.getElementById('myLinks');
EventUtil.addHandler(list,function(event){
var event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
switch(target.id){
case "doSomething":
document.title = "Changed title";
break;
case "goSomewhere":
window.open(
'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
'_blank' // <- This is what makes it open in a new window.
);
break;
case "sayHi":
alert('hi');
break;
}
});
Another case
1 | <div class="outter"> |
If we change the innerHtml of the outter div1
2
3
4
5var btn = document.getElementById('myBtn');
btn.onclick = function(){
var outterDiv = document.getElementById('outtter');
outterDiv.innerHtml = "processing........";
}
The listener of button is still there, so above code could be changed to1
2
3
4
5
6var btn = document.getElementById('myBtn');
btn.onclick = function(){
btn.onclick = null;
var outterDiv = document.getElementById('outtter');
outterDiv.innerHtml = "processing........";
}