2014-09-05 43 views
0

我想有一个页面上的多个Mapquest服务单张地图,但在小叶,你必须使用在div给出像这样得到Div编号为单张

 <div style="color:black ;" id="map"></div> 

的ID,然后

创建地图
 plugin.Map = new L.Map('map', {//<the id is used here 
       layers : [mapLayer], 
       center: new L.LatLng(39.828127,-98.579404), 
       boxZoom: true, 
       zoom: 5 
      }); 

,但我想知道是否有可能获得的ID,如果我有多个DIV这样的:

<div style="color:black; " id="map"></div> 
    <div style="color:black ;" id="map2"></div> 

和TH恩我可以叫

L.Map('map'...//where this is a possible array or something... 

的原因,我不能只是把IDS对自己是我创建了一个模板,我想这样做,没有硬编码什么...

回答

2

给他们的类名class="maps"和然后使用jquery

<div style="color:black ;" id="map1" class="maps"></div> 
<div style="color:black ;" id="map2" class="maps"></div> 
<div style="color:black ;" id="map3" class="maps"></div> 
<div style="color:black ;" id="map4" class="maps"></div> 

<script> 
    $().ready(function() { 
     $(".maps").each(function(){ 
      var mapID = $(this).attr("id"); 

      // do something 

      plugin.Map = new L.Map(mapID, {//<the id is used here 
        layers : [mapLayer], 
        center: new L.LatLng(39.828127,-98.579404), 
        boxZoom: true, 
        zoom: 5 
       }); 
     }); 
    ); 
</script> 

一些资源:

jquery get all form elements: input, textarea & select

http://api.jquery.com/jquery.each/

+1

而不是添加类,如果他们都在一个叫股利和'maps'您可以用'$(“#DIV地图”)'与上述溶液 – cantsay 2014-09-05 23:44:36

+0

这也是一种可能性。 – HelpNeeder 2014-09-05 23:45:44

+0

这似乎是工作,但现在,既没有地图是正确的,并且当我改变一个层,另一个灰色,是否有可能有两个地图正在实施像这样? – user3825831 2014-09-08 16:17:05