If you look at a Web page as a form, it is generally a model of a Web form. In the win form, it is easy to manually trigger an event for an object, as long as a message is sent. (PostMessage) But the Web page is not based on the message mechanism, if we want to write a page on a similar to the key wizard function how to implement it?
For everyone to introduce JS under a few methods:
1. CreateEvent (EventType)
Parameters: EventType A total of 5 types:
Events: Includes all the events.
Htmlevents: Includes ' abort ', ' blur ', ' Change ', ' error ', ' Focus ', ' load ', ' reset ', ' resize ', ' scroll ', ' SELECT ',
' Submit ', ' unload '. Event
Uievents: Including ' domactivate ', ' domfocusin ', ' domfocusout ', ' KeyDown ', ' keypress ', ' KeyUp '.
Indirectly contains mouseevents.
Mouseevents: Includes ' click ', ' MouseDown ', ' MouseMove ', ' mouseout ', ' mouseover ', ' MouseUp '.
Mutationevents: Including ' domattrmodified ', ' domnodeinserted ', ' domnoderemoved ',
' Domcharacterdatamodified ', ' domnodeinsertedintodocument ',
' Domnoderemovedfromdocument ', ' domsubtreemodified '.
2. After the CreateEvent must be initialized, for you to introduce 5 kinds of corresponding initialization method
Htmlevents and General Events:
Initevent (' type ', Bubbles, cancelable)
Uievents:
Inituievent (' type ', Bubbles, cancelable, windowobject, detail)
Mouseevents:
Initmouseevent (' type ', Bubbles, cancelable, windowobject, detail, ScreenX, ScreenY,
ClientX, ClientY, Ctrlkey, Altkey, Shiftkey, Metakey, button, Relatedtarget)
Mutationevents:
Initmutationevent (' type ', Bubbles, cancelable, Relatednode, Prevvalue, NewValue,
Attrname, Attrchange)
3. After the initialization is complete, you can trigger the required events at any time, to introduce Targetobj.dispatchevent (event)
Triggering event events for Targetobj objects
Note that in the IE 5.5+ version, please use the FireEvent method, or browse compatibility considerations
4. Example
Example 1 immediate trigger mouse down event
varFireonthis = document.getelementbyidx_x (' Someid ')); varEvobj = document.createevent (' mouseevents ')); Evobj.initmouseevent (' Click ',true,true, window, 1, 12, 345, 7, 220,false,false,true,false, 0,NULL ); Fireonthis.dispatchevent (Evobj); //Example 2 A mouse movement event that considers compatibility varFireonthis = document.getelementbyidx_x (' Someid ')); if(document.createevent) {varEvobj = document.createevent (' mouseevents ')); Evobj.initevent (' MouseMove ',true,false ); Fireonthis.dispatchevent (Evobj); } Else if(document.createeventobject) {fireonthis.fireevent (' OnMouseMove '); }
<!DOCTYPE HTML><HTMLLang= "ZH-CN"><Head> <MetaCharSet= "Utf-8"> <Metaname= "Viewport"content= "maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"> <Metaname= "Format-detection"content= "Telephone=no,email=no,date=no,address=no"> <title></title> </Head><Body> <DivID= "a"onclick= "alert (1);">321321</Div> <Scripttype= "Text/javascript"> /** * 1. CreateEvent (eventtype) * Parameters: EventType total of 5 Types * Events: Contains all events * Htmlevetns: ' abort ', ' bl ur ', ' change ', ' error ', ' Focus ', * ' load ', ' reset ', ' resize ', ' scroll ', ' SELECT ', * ' Submit ', ' unload ' * uievents: ' domactivate ', ' domfocusin ', ' domfocusout ', ' KeyDown ', ' keypress ', ' K Eyup ' * mouseevents: ' click ', ' MouseDown ', ' MouseMove ', ' mouseout ', ' mouseover ', ' mouseup ' * M Utationevents: ' domattrmodified ', ' domnodeinserted ', ' domnoderemoved ', ' domcharacterdatamodified ', * ' Domnodeinsertedintodocument ', ' domnoderemovedfromdocument ', ' domsubtreemodified ' *//** * 2 . After the CreateEvent must be initialized, for you to introduce 5 corresponding initialization methods * Htmlevents and General Events * initevent (' type ', bubbles,cancelabl E) * uievents * inituievent (' type ', bubbles,cancelable,windowobject,detail) * MouseEventS * initmouseevent (' type ', bubbles,cancelable,windowobject,detail,screenx,screeny,clientx,clienty,ctrlkey,a Ltkey,shiftkey,metakey,button,relatedtarget) * mutationevents * initmutationevent (' type ', bubbles,c Ancelable,relatednode,prevvalue,newvalue,attrname,attrchange) *//** * After the initialization is complete, you can trigger the required events at any time, to introduce t Argetobj.dispatchevent (Event) * To trigger event events for Targetobj objects * Note that the FireEvent method or browser-compatible consideration is required on the ie5.5+ version. *///immediately trigger mouse down event var fireonthis = document.getElementById (' a '); var evobj = document.createevent (' mouseevents '); Evobj.initmouseevent (' click ', True,true,window,1,12,345,7,220,false,false,true,false,0,null); Fireonthis.dispatchevent (Evobj);</Script></Body></HTML>
"Go" using the JS trigger event