简体   繁体   English

js事件和DOM事件之间有什么区别?

[英]what is the difference between js events and DOM events?

I am trying to understand events(such as: click, keyPress...) in js. 我试图在js中理解事件(例如:click,keyPress ...)。 But when I studied online, I saw it mentioned a lot on 'DOM events'. 但是当我在网上学习时,我看到它在“DOM事件”中提到了很多。 So my question is js events the same as DOM events? 所以我的问题是js事件和DOM事件一样吗? If not, what is the difference? 如果没有,有什么区别?

DOM events fires when the DOM changes or interacts with user, and they are Javascript events. 当DOM更改或与用户交互时,DOM事件将触发,并且它们是Javascript事件。 Please have a read all of them: http://www.w3schools.com/jsref/dom_obj_event.asp 请阅读所有这些内容: http//www.w3schools.com/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. 除了DOM事件,您可以在Javascript中定义自己的事件对象,并使用'dispatchEvent'方法来触发该事件。 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. 简而言之,您可以认为DOM事件是从DOM元素触发的本机Javascript事件。 While a Javascript event can be a DOM event or Custom event 虽然Javascript事件可以是DOM事件或自定义事件

A DOM event is any event that elements or objects in the DOM listen on. DOM事件是DOM中的元素或对象侦听的任何事件。 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事件由某种用户交互(鼠标事件,键盘事件,表单提交等)触发。 DOM events can be triggered programatically though. 但是,可以以编程方式触发DOM事件。

There are other events that wouldn't be regarded as DOM events, for example: 还有其他事件不会被视为DOM事件,例如:

  • AJAX response ( onreadystatechange ) AJAX响应( onreadystatechange
  • WebSocket message received ( MessageEvent ) 收到WebSocket消息( MessageEvent
  • LocalStorage data changed ( storage ) LocalStorage数据已更改( storage

Dom Events: This event perform on the DOM component to perform certain action over it like (events/properties,etc) Dom事件:此事件在DOM组件上执行以对其执行某些操作,如(事件/属性等)

Js Events: This events will perform action over the content of the DOM object like (validation(condition),expression,methods over the Dom object,etc) Js事件:此事件将对DOM对象的内容执行操作,如(验证(条件),表达式,Dom对象上的方法等)

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. 在js中,DOM事件是标准事件,对应于直接对DOM中的元素执行的操作,例如当用户点击某些内容时,触发click事件(直接对应于用户单击按钮)并且任何事件处理程序附加到该元素将被调用。 Here's a list of events: https://en.wikipedia.org/wiki/DOM_events These are recognized and supported by the browsers and are natively triggered. 以下是一系列事件: https//en.wikipedia.org/wiki/DOM_events这些都是浏览器识别和支持的,并且是本机触发的。

Many js libraries make their own system of events over these DOM events. 许多js库通过这些DOM事件创建自己的事件系统。 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 他们还可以通过让用户调用特定的函数来支持DOM或任何其他对象上的自定义事件

 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. 因此,只要触发receive事件,就会调用匿名函数(显示警报)。 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 这里发生的是on函数将保留一个附加到对象的处理程序列表,触发器函数将调用每个处理程序并使用任何所需的数据调用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM