javascript 你如何向传单标记添加一个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27267411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add a class to a Leaflet marker?
提问by yesman
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:
我正在为我的传单标记使用自定义 divIcons。我想用一些简单的 CSS 为我点击的任何标记添加边框:
.selectedMarker {
border: 10px solid gold;
}
However, the following with jQuery doesn't work:
但是,以下使用 jQuery 不起作用:
$(marker).addClass('selectedMarker');
Then I tried to use Leaflet's own addClass() method. I tried to call use it in the following ways:
然后我尝试使用 Leaflet 自己的addClass() 方法。我尝试通过以下方式调用使用它:
marker.addClass('selectedMarker');
L.addClass(marker, 'selectedMarker');
addClass(marker, 'selectedMarker');
DomUtil.addClass(marker, 'selectedMarker');
None of these work. How do I add the selectedMarker class to my marker?
这些都不起作用。如何将 selectedMarker 类添加到我的标记中?
采纳答案by r8n5n
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
然后使用css
.leaflet-marker-icon.selectedMarker{
//your css
}
回答by bgeo
回答by Mujtaba Kably
without using jQuery,
不使用jQuery,
marker._icon.classList.add("className");