JavaScript Event handlers (event listeners) _javascript tips

Source: Internet
Author: User
Tags event listener
We know that the interaction between JavaScript and HTML is done through events, something that the user or the browser itself performs, such as Click, Mounseover, Load ..., and the function that responds to the event is called the event handler function (or event listener).
<ptml> <pead> <title>event util test</title> <script type= "Text/javascript" > Var event Util = {addlistener:function (element, type, hander) {if (Element.addeventlistener) {Element.addeventlistener (type), hander, false); else if (element.attachevent) {element.attachevent (' on ' + type, hander); else {element[' on ' + type] = hander; }, Getevent:function (event) {return event | | window.event; Return event? Event:window.event; }, Gettarget:function (event) {return Event.target | | event.srcelement; }, Preventdefault:function (event) {if (Event.preventdefault) {event.preventdefault (); else {event.returnvalue = false; }, Removelistener:function (element, type, hander) {if (Element.removeeventlistener) {Element.removeeventlistener ( Type, hander, false); else if (element.deattachevent) {element.detachevent (type, hander); else {element[' on ' + type] = NULL; }}, Stoppropagation:function (event) {if (event.stoppropagation) {event.stoppropagation (); else {event.cancelbubble = true; } } }; </script> </pead> <body> <input type= "button" value= "click Me" id= "btn"/> Baidu <script t Ype= "Text/javascript" > (function () {var btn = document.getElementById ("btn"); var link = document.getelementsbytagname ("a") [0]; Eventutil.addlistener (Link, "click", Function (event) {alert ("Prevent default event"); var event = eventutil.getevent (event); Eventutil.preventdefault (event); }); Eventutil.addlistener (BTN, "click", Function (event) {var event = eventutil.getevent (event); var target = Eventutil.gettarget (event); alert (Event.type); alert (target); Eventutil.stoppropagation (event); }); Eventutil.addlistener (Document.body, "click", Function () {alert ("click Body"); }); })(); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.