<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="button" value="按钮" id="btn"> <script src="common.js"></script> <=btnwas>script my$('btn'); function btnClick() { alert('hello'); // 移除事件 removeEventListener(btn, 'click', btnClick); } addEventListener(btn, 'click', btnClick); </script> </body> </html>
common codes
function My $ (ID) { return document.getElementById (ID); } // process browser compatibility // Get first child function getFirstElementChild (Element) { var Node, Nodes = the element.childNodes, I = 0 ; the while (Node = Nodes [I ++ ]) { IF (node.nodeType ===. 1 ) { return Node; } } return null ; } // process browser compatibility // Get the next sibling function getNextElementSibling (element) { var EL =Element; the while (EL = el.nextSibling) { IF (el.nodeType ===. 1 ) { return EL; } } return null ; } // compatibility problems and processing innerText textContent of // the content between the tags set function setInnerText (Element, Content) { // determines whether the current browser support the innerText iF ( typeof element.innerText === 'String' ) { element.innerText = Content; } the else { element.textContent = Content; } } // compatibility issues deal with registered events // eventName, without ON, the Click mouseOver mouseout function addEventListener (Element, eventName, the Fn) { // determine whether the current browser supports addEventListener method IF (element.addEventListener) { element.addEventListener (eventName, Fn); // third parameter defaults to false } the else IF (element.attachEvent) { element.attachEvent ( 'ON' + eventName, Fn); } the else { // corresponds element.onclick = fn; Element [ 'ON' eventName +] = Fn; } } // process removes compatibility event handling function the removeEventListener (Element, eventName, Fn) { IF (element.removeEventListener) { element.removeEventListener (eventName, Fn); } the else IF (element.detachEvent) { element.detachEvent ( 'ON' + eventName, Fn); } the else { Element [ 'ON' eventName +] = null ; } } // Get browser compatibility issues page scrolling distance // Get out from the page scrolling function getScroll () { var the scrollLeft = || document.body.scrollLeft Document .documentElement.scrollLeft; var= || document.body.scrollTop scrollTopdocument.documentElement.scrollTop; return { the scrollLeft: the scrollLeft, scrollTop: scrollTop } } // Get the mouse position on the page, the browser compatibility processing function the getPage (E) { var the pageX e.clientX || + = e.pageX getScroll () .scrollLeft; var pageY e.clientY || + = e.pageY getScroll () scrollTop;. return { the pageX: the pageX, pageY: pageY } }