Leaflet path: how can I set a css class?

后端 未结 6 1468
一个人的身影
一个人的身影 2021-01-07 23:13

Well the title says it all but here is some code so you see what i mean.

function eachFeature(feature, layer) {
     layer.on({
         mouseover: highlight         


        
6条回答
  •  醉话见心
    2021-01-08 00:05

    Something like this should do the trick. feature._container exposes the underlying DOM element, so you can use regular DOM operations on it (e.g. classList on modern browsers, or setAttribute('class', 'someClass') in older browsers).

    function eachFeature(feature, layer) {
         // Need to use setTimeout hack so the DOM container will be
         // available.  WARNING: may cause race condition issues,
         // maybe tie into some other timing event?
         window.setTimeout(function() {
             feature._container && feature._container.classList.add('someClass');
         }, 0);
    }
    geojson = L.geoJson(geojson_raw, { onEachFeature: eachFeature });
    geojson.addTo(map);
    

提交回复
热议问题