JavaScript custom events, adding properties dynamically

Source: Internet
Author: User

Depending on the event, the available customization methods are different.

Document.createevent (' Event ');

There are 4 main steps to achieve:

1. Create the event.

2. Initialize event (three parameters: event name, whether blistering, whether to cancel default trigger)

3. Monitoring Events

4. Triggering events

var Evt = document.createevent (' Event '); // Create an event true true); // Initialize the event, given the name of the event function (event) {// listen to the custom event         console.log (event);    }); Window.dispatchevent (EVT); // triggering custom INPUTCHANGEEVT events

Concrete implementation.

<!DOCTYPE HTML><HTML><Head>    <MetaCharSet=utf-8 "/>    <title>By:dragondean</title>    <Scripttype= "Application/javascript">window.onload=function(){            varevt=Document.createevent ('Event'); Evt.initevent ('MyEvent',true,true); varobj=document.getElementById ('Testbox'); Obj.addeventlistener ('MyEvent', function() {Console.log (' MyEvent events triggered the'); }, false);        Obj.dispatchevent (EVT);    }; </Script></Head><Body><DivID= "Testbox">Ddddd</Div></Body></HTML>

Of course, the custom event is definitely not that simple, and the job is just a custom event for me to send data to other JS if data changes occur. It involves the dynamic addition of attributes, event passing values, and so on.

/** * Created by V_zweiwang on 2015/10/26.*/window.onload=function() {Window.addeventlistener (' Inputchange ',function(event) {//Listen, this custom event is not moving.Console.log (Event.detail);    }); Mybord.init ();//Use};(function() {window[' Mybord '] = {}; functioninit () {mybord.inputchangeevt= Document.createevent (' Event ');//Create an eventMyBord.inputChangeEvt.initEvent (' Inputchange ',true,true);//Initialize the event, given the name of the event        //Initialize the dataMyBord.inputChangeEvt.detail ={TranslateX:' 0.00 ', Translatey:' 0.00 ', Translatez:' 0.00 ', Rotatex:' 0.00 ', Rotatey:' 0.00 ', Rotatez:' 0.00 ', Scale:' 1.00 '        }; Try{window.dispatchevent (mybord.inputchangeevt);//triggering custom INPUTCHANGEEVT events}Catch(e) {console.error (e); }} window[' Mybord ' [' init '] =init;}) ();

At this point, the event can already be used to pass the value. Directly copy the above code into HTML, run, keyboard F12, you can see the following output.

OK, now let's talk about realization.

(Explanation code)

How to add properties dynamically.

How to use events to pass values.

JavaScript custom events, adding properties dynamically

Related Article

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: 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.