/ Published in: JavaScript
IE 5/6 does not support the :hover pseudo-class on any element other than links. This script attaches a .hover class to mimic the :hover pseudo class on IE 5/6.
Expand |
Embed | Plain Text
function addIEHover(arg) { // check to see if browser supports mouseenter event if not return if(typeof document.body.onmouseenter != "object") return; // if passed argument not an array convert it to one if(!arg.length) { var elems = []; elems[0] = arg; } else { var elems = arg; } // iterate through array and attach mouseenter event to obejcts passed // add and remove class="hover" on mousenter/mouseout event for (var i = 0; i < elems.length; i++) { elems[i].onmouseenter = function() { this.className += " hover"; } elems[i].onmouseleave = function() { this.className = this.className.replace(/\s*hover/, ""); } } } // call function in one of the following Manners addIEHover([document.getElementById("one"), document.getElementById("two")]); addIEHover(document.getElementsByTagName("div")); addIEHover(document.getElementsByTagName("*")); //attach hover class to all element nodes //CSS /* nav:hover, nav.hover { styles to apply on hover } */
You need to login to post a comment.
