AddEventHandler() In A Loop Has Unexpected Results
I wasn't sure what to look for, so I couldn't find any previous questions on this topic, and Google didn't help much, either. (function() { var element = function(str) {
Solution 1:
Variables in javascript are function-scope even when you declare them inside a block. This loop creates a closure around the variable loop, but loop is reassigned on every iteration. By the time the loop is done, all the closures point to the last element in the array.
To fix this, scope the variable differently:
(function() {
var element = function(str) {
return document.getElementById(str);
},
parent = document.getElementsByTagName('ul')[0].getElementsByTagName('li'),
len = parent.length;
for (var i=0; i<len; i++) {
var link = parent[i].getElementsByTagName('a')[0],
slide = element(parent[i].getElementsByTagName('a')[0].getAttribute('href').substr(1));
if (addEventListener) {
link.addEventListener('click', (function(slide){
return function(event) {
event.preventDefault();
alert(slide.getAttribute('id'))
}
})(slide));
} else if (attachEvent) {
}
}
})();
Solution 2:
I have encoutered this bug before, it is related to the binding of the slide, which is very difficult to explain. Here is a fix.
link.addEventListener('click',
(function(x) {
return function(event){
event.preventDefault();
alert(x.getAttribute('id'));
}
})(slide));
Post a Comment for "AddEventHandler() In A Loop Has Unexpected Results"