2013-06-26 47 views
-2

我在jshint.com中測試了這段代碼。在ie8和jshint中抱怨L是未定義的。爲什麼L未定義,我如何定義它,所以ie8將與此代碼一起工作?L未定義ie8

你可以在這裏找到這個例子的代碼。 http://leafletjs.com/examples/choropleth.html

var map = L.map('map').setView([37.8, -96], 4); 

    var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', { 
     attribution: 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade', 
     key: 'BC9A493B41014CAABB98F0471D759707', 
     styleId: 22677 
    }).addTo(map); 


    // control that shows state info on hover 
    var info = L.control(); 

    info.onAdd = function (map) { 
     this._div = L.DomUtil.create('div', 'info'); 
     this.update(); 
     return this._div; 
    }; 

    info.update = function (props) { 
     this._div.innerHTML = '<h4>US Population Density</h4>' + (props ? 
      '<b>' + props.name + '</b><br />' + props.density + ' people/mi<sup>2</sup>' 
      : 'Hover over a state'); 
    }; 

    info.addTo(map); 


    // get color depending on population density value 
    function getColor(d) { 
     return d > 1000 ? '#800026' : 
       d > 500 ? '#BD0026' : 
       d > 200 ? '#E31A1C' : 
       d > 100 ? '#FC4E2A' : 
       d > 50 ? '#FD8D3C' : 
       d > 20 ? '#FEB24C' : 
       d > 10 ? '#FED976' : 
          '#FFEDA0'; 
    } 

    function style(feature) { 
     return { 
      weight: 2, 
      opacity: 1, 
      color: 'white', 
      dashArray: '3', 
      fillOpacity: 0.7, 
      fillColor: getColor(feature.properties.density) 
     }; 
    } 

    function highlightFeature(e) { 
     var layer = e.target; 

     layer.setStyle({ 
      weight: 5, 
      color: '#666', 
      dashArray: '', 
      fillOpacity: 0.7 
     }); 

     if (!L.Browser.ie && !L.Browser.opera) { 
      layer.bringToFront(); 
     } 

     info.update(layer.feature.properties); 
    } 

    var geojson; 

    function resetHighlight(e) { 
     geojson.resetStyle(e.target); 
     info.update(); 
    } 

    function zoomToFeature(e) { 
     map.fitBounds(e.target.getBounds()); 
    } 

    function onEachFeature(feature, layer) { 
     layer.on({ 
      mouseover: highlightFeature, 
      mouseout: resetHighlight, 
      click: zoomToFeature 
     }); 
    } 

    geojson = L.geoJson(statesData, { 
     style: style, 
     onEachFeature: onEachFeature 
    }).addTo(map); 

    map.attributionControl.addAttribution('Population data &#169; <a href="http://census.gov/">US Census Bureau</a>'); 


    var legend = L.control({position: 'bottomright'}); 

    legend.onAdd = function (map) { 

     var div = L.DomUtil.create('div', 'info legend'), 
      grades = [0, 10, 20, 50, 100, 200, 500, 1000], 
      labels = [], 
      from, to; 

     for (var i = 0; i < grades.length; i++) { 
      from = grades[i]; 
      to = grades[i + 1]; 

      labels.push(
       '<i style="background:' + getColor(from + 1) + '"></i> ' + 
       from + (to ? '&#8211;' + to : '+')); 
     } 

     div.innerHTML = labels.join('<br />'); 
     return div; 
    }; 

    legend.addTo(map); 
+0

既然你專門挑出來'IE8',我們應該假定它工作在其他瀏覽器? –

+0

@CrazyTrain是的,它可以在Firefox中工作。感謝您閱讀更多的問題。 – user1015711

+0

然後它似乎有加載庫的一些問題。你有你的'doctype'設置,並且你檢查了你沒有在兼容模式? –

回答

3

代碼很好。 JShint說L是未定義的,因爲L在包含傳單的js文件中聲明。

爲了解決IE8的問題,在頭頂部放:

<meta http-equiv="X-UA-Compatible" content="IE=Edge" /> 
2

L不確定。你沒有定義它。這就是「未定義」的含義。如果你想抑制錯誤,你必須告訴linter L是一個定義的符號。否則,請忽略該錯誤。

+3

對於JSHint,您可以在代碼頂部添加'/ * global L * /'。 –

+0

@meagar那我該如何定義它? – user1015711

+0

只需定義它:'var L;' – meagar

相關問題