admin管理员组

文章数量:1332896

Do you know a JS library for DOM events simulation? I know that this operation can be done but i can't find any library to do it.

UPDATE: I try to explain better my question. Javascript can simulate events like the user click, i'm looking for a library that helps me with this operation.

Do you know a JS library for DOM events simulation? I know that this operation can be done but i can't find any library to do it.

UPDATE: I try to explain better my question. Javascript can simulate events like the user click, i'm looking for a library that helps me with this operation.

Share Improve this question edited Jan 12, 2014 at 15:08 kangax 39.2k13 gold badges100 silver badges135 bronze badges asked Mar 26, 2010 at 16:01 mck89mck89 19.3k17 gold badges91 silver badges110 bronze badges 4
  • I mean javascript can simulate an event like the user click. I want to know if there's a library to simplify this operation. – mck89 Commented Mar 26, 2010 at 16:03
  • Please clarify if simply calling handlers attached to an element is good enough "simulation" for you. – Crescent Fresh Commented Mar 26, 2010 at 16:07
  • The event simulation allows you to specify some details to build the event object to pass into the hendlers, so it's a little more plex. – mck89 Commented Mar 26, 2010 at 16:19
  • yep, and some libraries (jQuery) simulate events with a fake event object and simply call the handlers attached to the element (faking out event bubbling too). Other libraries (eg Prototype, YUI) route to one of dispatchEvent/fireEvent with a real event object. There are pros and cons to both approaches. Which one are you looking for? – Crescent Fresh Commented Mar 26, 2010 at 16:31
Add a ment  | 

4 Answers 4

Reset to default 5

Kangax made a very useful set of extensions for the PrototypeJS framework, the one that might interest you is Event.simulate

http://developer.yahoo./yui/3/event/#eventsimulation

""" Simulated events are browser-created events that, most of the time, behave exactly as user-initated events. Events bubble as they normally would and event objects are created with properties containing data about the event (sometimes these properties are browser-specific, so it's remended that you make use of the browser-equalizing methods of Y.Event to retrieve the appropriate values for properties such as target, relatedTarget, and charCode. All event handlers are called synchronously at each event target throughout the event's lifetime. Events are simulated using the simulate() method on any Y.Node instance. """

All of the major javascript libraries have the ability to trigger events. To add to the chorus of examples, in jquery you do it with: $("target_element").trigger("an_event");

You can use prototype.js to fire your own custom events

e.g. treeContainer.fire('nodeIcon:mousedown', {targetEvent: event, node: node});

本文标签: Javascript event simulation libraryStack Overflow