
Originally Posted by
Twey
Code:
<script type="text/javascript">
var t = document.getElementsByTagName("tr");
for(var i=0;i<t.length;i++) {
var ocn = t[i].className;
t[i].onmouseover = function() { t[i].className = "hovered" };
t[i].onmouseout = function() { t[i].className = ocn };
}
</script>
That's broken (and could be better written). The listeners will look-up the value of i when called, which will equal the length property of the collection (and yield undefined). The this operator (and feature detection) should be used instead:
Code:
if(document.getElementsByTagName) {
(function() {
var className = 'hovered',
pattern = new RegExp('(^|\\s+)' + className + '(\\s+|$)'),
rows = document.getElementsByTagName('tr');
for(var i = 0, n = rows.length; i < n; ++i) {
rows[i].onmouseover = function() {
this.className += ' ' + className;
};
rows[i].onmouseout = function() {
this.className = this.className.replace(pattern, ' ');
};
}
rows = null;
})();
}
Mike
Bookmarks