javascript - what is the difference between js events and DOM events? - Stack Overflow

I am trying to understand events(such as: click, keyPress...) in js. But when I studied online, I saw i

I am trying to understand events(such as: click, keyPress...) in js. But when I studied online, I saw it mentioned a lot on 'DOM events'. So my question is js events the same as DOM events? If not, what is the difference?

I am trying to understand events(such as: click, keyPress...) in js. But when I studied online, I saw it mentioned a lot on 'DOM events'. So my question is js events the same as DOM events? If not, what is the difference?

Share Improve this question edited Jun 25, 2013 at 7:30 user2294256 asked Jun 25, 2013 at 7:22 user2294256user2294256 1,0491 gold badge13 silver badges23 bronze badges 1
  • 3 There is no "official" thing as a "js event" so you'd have to explain exactly what you mean when you use that term. DOM events can be listened to via js. A given js library may create it's own event system in order to notify other pieces of it's own code about certain actions. – jfriend00 Commented Jun 25, 2013 at 7:27
Add a ment  | 

4 Answers 4

Reset to default 3

DOM events fires when the DOM changes or interacts with user, and they are Javascript events. Please have a read all of them: http://www.w3schools./jsref/dom_obj_event.asp

Apart from DOM events, you can define your own event objects in Javascript and use the 'dispatchEvent' method to fire that event. For example:

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

In short, you can think of DOM events are native Javascript events that fires from DOM elements. While a Javascript event can be a DOM event or Custom event

A DOM event is any event that elements or objects in the DOM listen on. For example, a button click, a text input keypress, a mouseover. Generally DOM events are triggered by some sort of user interaction (mouse events, keyboard events, form submissions etc). DOM events can be triggered programatically though.

There are other events that wouldn't be regarded as DOM events, for example:

  • AJAX response (onreadystatechange)
  • WebSocket message received (MessageEvent)
  • LocalStorage data changed (storage)

An event is a system when certain actions performed are recorded and can be acted upon. Such as clicking on a button, hovering over some box, selecting some text, or even receiving a new message on a certain channel.

In js, DOM events are standard events that correspond to actions performed directly on an element in the DOM such as when the user clicks on something, the click event(directly corresponding to the user clicking the button) is triggered and any event handlers attached to the element will be called. Here's a list of events: https://en.wikipedia/wiki/DOM_events These are recognized and supported by the browsers and are natively triggered.

Many js libraries make their own system of events over these DOM events. They wrap around the element that is listened to and when an event is triggered, they propogate the event to the handling function

They can also support custom events on DOM or any other object by having the user call a specific function such as

 obj.on("receive", function(){alert("Hello")})


 //Later
 obj.trigger("receive")

So the anonymous function(display an alert) will be called whenever you trigger the receive event. What happens here is that the on function will keep a list of handlers attached to the object and the trigger function will call each one and call them using any required data

Dom Events: This event perform on the DOM ponent to perform certain action over it like (events/properties,etc)

Js Events: This events will perform action over the content of the DOM object like (validation(condition),expression,methods over the Dom object,etc)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744211903a4563373.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信