简体   繁体   中英

Passing multiple variables to an event listener

I am working with the Google Maps API and multiple markers. On a marker, there needs to be a click event to open an info window. I got that part working. I was wondering if there was a way to pass multiple variables and add the same event listener on all those markers?

I currently have:

 google.maps.event.addListener(marker, 'click', function () { _(that.popups).each(function (popup) { popup.hide(); }).value(); infowindow.show(/*that.map, marker*/); });

If I have a nyMarker variable, then a laMarker variable, how do I pass on this to the event handler?

I am working with the Google Maps API and multiple markers. On a marker, there needs to be a click event to open an info window. I got that part working. I was wondering if there was a way to pass multiple variables and add the same event listener on all those markers?

I currently have:

 google.maps.event.addListener(marker, 'click', function () { _(that.popups).each(function (popup) { popup.hide(); }).value(); infowindow.show(/*that.map, marker*/); });

If I have a nyMarker variable, then a laMarker variable, how do I pass on this to the event handler?

Its very simple; there are two methods as you want to do, both of them work; what you have to do is just use forEach()<\/strong> like this -

    1. <\/li><\/ol>

       const element1 = document.querySelector('.a'); const element2 = document.querySelector('.b'); const element3 = document.querySelector('.c');<\/code><\/pre>

        <\/li><\/ol>

         [element1, element2, element3].forEach((element)=>{ element.addEventListner('click', ()=>{ console.log('action'); }); });<\/code><\/pre>

        "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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