<HTML> <body> <button id = "mybutton" onclick = "changebg ();"> click to change color </button> <button onclick = "detachfunction (); "> detachfunction </button> <script language =" JavaScript "> function changebg () {document. bgcolor = 'red';} function detachfunction () {document. bgcolor = 'white'; document. getelementbyid ("mybutton "). detachevent ('onclick', changebg) ;}</SCRIPT> </body>
Ie9 cannot use detachevent to unbind events written in HTML tags.
Only events that are dynamically bound can be dynamically unbound.
<HTML> <body> <button id = "mybutton"> click to change color </button> <button onclick = "attachfunction (); "> attachfunction </button> <button onclick =" detachfunction (); "> detachfunction </button> <script language =" JavaScript "> function changebg () {document. bgcolor = 'red';} function attachfunction () {document. getelementbyid ("mybutton "). attachevent ("onclick", changebg);} function detachfunction () {document. bgcolor = 'white'; document. getelementbyid ("mybutton "). detachevent ('onclick', changebg) ;}</SCRIPT> </body>