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