Actually, a CSS hover event is more convenient than just binding these two events, mouseenter and mouseleave . Therefore, a little more effort is required that:
1.Click an item
2. Add a listener to the mouseenter event
3. Repeat step 1 , 2 again and restore the cloned item to mouseleave
Here is my working draft.
function bindHoverEvent(element,listener){ var originalElement = element.cloneNode(true); element.addEventListener('mouseenter',listener); element.addEventListener('mouseleave',function(){ bindHoverEvent(originalElement,listener); this.parentNode.replaceChild(originalElement,this); }); }
Note that cloneNode does not copy event listeners, so you need to manually rewrite events to the cloned element and all its children yourself.
source share