Leaflet path: how can I set a css class?

后端 未结 6 1424
一个人的身影
一个人的身影 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:01

    Expanding on @tbicr s answer, you should be able to do something like this:

    function onEachFeature(feature, path) {
      path.on('click', addClass);
    }
    
    function addClass(e){
        var path = e.target;
        var container = path._container;
        $(container).attr('class', 'test');
    }
    
    geojson = L.geoJson(geojson_raw, { style: style, onEachFeature: eachFeature });
    geojson.addTo(map);
    

    EDIT: As always, I would be glad flor an explanation, if you decide to downvote, so I can improve it. This is the only answer so far, which describes how to add a class dynamically in detail and I can't see whats wrong with it.

提交回复
热议问题