Dynamically Adding Listeners To Google Maps Markers

前端 未结 2 1153
情深已故
情深已故 2021-01-12 13:49

I\'m working on a page which fetches code with a Javascript httpObject and uses it to update two elements on the page - a google map, and a DIV that lists the things the mar

相关标签:
2条回答
  • 2021-01-12 14:20

    Closure issue -- all those listeners share the same dinnerNumber variable. Try this:

    GEvent.addListener(newMarkers[count], 'mouseover', (function(dinnerNumber){ return function(){document.getElementById(dinnerNumber).style.borderColor = '#000000';}; })(dinnerNumber));
    

    This way, each listener is created with its own closed copy of dinnerNumber.

    0 讨论(0)
  • You have to read carefully

    GEvent.addListener(newMarkers[count], 'mouseover', 
           (function(dinnerNumber)
              { return function()
                    { document.getElementById(dinnerNumber).style.borderColor = '#000000';};        
              }
          )(dinnerNumber)
    );
    

    you missed one ();

    the 3-rd parameter is (function(var){return function(){//what you want wirh var;};})(var)

    0 讨论(0)
提交回复
热议问题