Retrieving JavaScript's eventObject when using

2019-06-07 22:35发布

In google maps API V3, I would like to add a marker to the map if the user control-clicked the map. For that, I've added a listener to the map, as follows -

google.maps.event.addListener(map, 'click', function(e){
if (event.ctrlKey)
    add_marker(e.position);
});

The e parameter, passed by the listener, contains some data, but mostly regarding the position of the click, while I want to be able to ask if the control button was pressed during the time the usser clicked on the map.

I found that chrome had an object event, which is the default Javascript's eventObject, that contained the data I needed (ctrlKey) and this indeed works in chrome.

However, when I tried the same code in FF, it couldn't find an object called 'event', and I can't find a way to retrieve it.

I would appreciate your help in finding a solution that will work on IE too.

Thank, DanC

1条回答
Root(大扎)
2楼-- · 2019-06-07 23:13

The API doesn't say anything about accessing the DOM-event-object.

The argument passed to the callback-function currently contains a property b which refers to the event-object, so you could use e.b.ctrlKey

But as this is not documented it's not reliable may change tomorrow.

Another option:
You may observe the event for the div that contains the map without using the API-method:

map.getDiv().onclick=function(e)
{
  e=window.event||e;
  if (e.ctrlKey)
  {
    //do something
  }
}
查看更多
登录 后发表回答