如何将类添加到传单标记?

2022-01-12 00:00:00 leaflet jquery javascript

我正在为传单标记使用自定义 divIcon.我想用一些简单的 CSS 为我点击的任何标记添加边框:

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 solid gold;
}

但是,以下 jQuery 不起作用:

However, the following with jQuery doesn't work:

$(marker).addClass('selectedMarker');

然后我尝试使用 Leaflet 自己的 addClass() 方法.我尝试通过以下方式调用使用它:

Then I tried to use Leaflet's own addClass() method. I tried to call use it in the following ways:

marker.addClass('selectedMarker');
L.addClass(marker, 'selectedMarker');
addClass(marker, 'selectedMarker');
DomUtil.addClass(marker, 'selectedMarker');

这些都不起作用.如何将 selectedMarker 类添加到我的标记中?

None of these work. How do I add the selectedMarker class to my marker?

推荐答案

我已经通过在标记中添加一个类来实现

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');
}

然后使用css

.leaflet-marker-icon.selectedMarker{
  //your css
}

相关文章