How do you add a class to a Leaflet marker?

后端 未结 3 434
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 08:39

I\'m using custom divIcons for my Leaflet markers. I want to add a border to whatever marker I click on, with some simple CSS:

.selectedMarker {
border: 10px         


        
相关标签:
3条回答
  • 2020-12-17 09:23

    In 1.0 and 0.7 you can use L.DomUtil to add remove classes from a DOM element:

    L.DomUtil.addClass(marker._icon, 'className');
    L.DomUtil.removeClass(marker._icon, 'className');
    
    0 讨论(0)
  • 2020-12-17 09:24

    I have done it by adding a class to the marker with

    var marker = L.marker(loc);
    marker.on('click', function() {
        $(marker._icon).addClass('selectedMarker');
    }
    

    and then use the css

    .leaflet-marker-icon.selectedMarker{
      //your css
    }
    
    0 讨论(0)
  • 2020-12-17 09:30

    without using jQuery,

    marker._icon.classList.add("className");
    
    0 讨论(0)
提交回复
热议问题