A set of methods shared between event-powered classes (like Map
and Marker
). Generally, events allow you to execute some function when something happens with an object (e.g. the user clicks on the map, causing the map to fire 'click'
event).
一组在事件驱动类(如Map和Marker)之间共享的方法。通常,事件允许您在对象发生某些情况时执行某些功能(例如,用户单击地图,导致地图触发“单击”事件)。
Usage example
map.on('click', function(e) {
alert(e.latlng);
} );
Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:
Leaflet 通过引用处理事件侦听器,因此如果您想添加侦听器然后删除它,请将其定义为函数:
function onClick(e) { ... }
map.on('click', onClick);
map.off('click', onClick);
Methods
Method | Returns | Description |
---|---|---|
on(<String> type, <Function>
|
this |
Adds a listener function (fn ) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick' ).
向对象的特定事件类型添加侦听器函数(fn)。您可以选择指定侦听器的上下文(this关键字将指向的对象)。您还可以传递几个空格分隔的类型(例如“click dblclick”)。 |
on(<Object> eventMap) |
this |
Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove}
添加一组类型/侦听器对,例如{click:onClick,mousemove:onMouseMove} |
off(<String> type, <Function>
|
this |
Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to on , you must pass the same context to off in order to remove the listener.
删除以前添加的侦听器函数。如果没有指定函数,它将从对象中删除该特定事件的所有侦听器。请注意,如果您将自定义上下文传递给on,则必须将相同的上下文传递给off才能删除侦听器。 |
off(<Object> eventMap) |
this |
Removes a set of type/listener pairs.
删除一组类型/侦听器对。 |
off() |
this |
Removes all listeners to all events on the object. This includes implicitly attached events.
删除对象上所有事件的所有侦听器。这包括隐式附加的事件。 |
fire(<String> type, <Object>
|
this |
Fires an event of the specified type. You can optionally provide a data object — the first argument of the listener function will contain its properties. The event can optionally be propagated to event parents.
激发指定类型的事件。您可以选择提供一个数据对象-侦听器函数的第一个参数将包含其属性。可以选择将事件传播到事件父级。 |
listens(<String> type,
|
Boolean |
Returns true if a particular event type has any listeners attached to it. The verification can optionally be propagated, it will return true if parents have the listener attached to it.
如果特定事件类型附加了任何侦听器,则返回true。可以选择传播验证,如果父级附加了侦听器,验证将返回true。 |
once(…) |
this |
Behaves as on(…) , except the listener will only get fired once and then removed.
表现为on(…),但监听器只会被解雇一次,然后被移除。 |
addEventParent(<Evented> obj) |
this |
Adds an event parent – an Evented that will receive propagated events
添加事件父级-将接收传播事件的Evented |
removeEventParent(<Evented> obj) |
this |
Removes an event parent, so it will stop receiving propagated events
删除事件父级,因此它将停止接收传播的事件 |
addEventListener(…) |
this |
Alias to on(…) |
removeEventListener(…) |
this |
Alias to off(…) |
clearAllEventListeners(…) |
this |
Alias to off() |
addOneTimeEventListener(…) |
this |
Alias to once(…) |
fireEvent(…) |
this |
Alias to fire(…) |
hasEventListeners(…) |
Boolean |
Alias to listens(…) |