简体   繁体   English

传单-单击将类添加到标记图标

[英]Leaflet - add class to marker icon on click

I have a simple leaflet map and some markers on it. 我有一个简单的传单地图,上面有一些标记。 To give the user a better experience i want to highlight a clicked marker. 为了给用户带来更好的体验,我想突出显示单击的标记。 The best and simplest would be if i could just add an additional class to the clicked marker to work on css with it. 最好和最简单的方法是,如果我可以将其他类添加到单击的标记中,以在CSS上使用它。 But i dont know how and all i found wasn't answering my question or i just didn't understood it. 但是我不知道怎么做,我发现的全部都没有回答我的问题,或者我只是不理解。

Javascript Java脚本

//Set the marker icon

    var markerIcon = L.vectorIcon({
            className: 'markerIcon',
            svgHeight: 30,
            svgWidth: 30,
            shape: {r: '15', cx: '15', cy: '15'},
            style: {
                fill: '#73B0E1'
            }
    });


//Populate the map with markers

    var markers = L.markerClusterGroup({});
    if(mapID == 'mapPublic') {
        markers.addLayer(L.marker([50, 8], {icon: markerIcon})).on('click', onClick);
        markers.addLayer(L.marker([50, 8.1], {icon: markerIcon})).on('click', onClick);
        markers.addLayer(L.marker([50, 8.2], {icon: markerIcon})).on('click', onClick);
        markers.addLayer(L.marker([50.1, 8.1], {icon: markerIcon})).on('click', onClick);
    }

    map.addLayer(markers);


//Onclick Function for the markers

    function onClick() {}

How can i add a second class to the marker to work in css with it? 我如何向标记添加第二类以在CSS中使用它? I allready tried this.classList.add('activeMarker') but it didn't worked, i think because of leaflets marker logic. 我已经尝试过this.classList.add('activeMarker')但是由于传单标记逻辑,它没有起作用。

The marker isn't a DOM object so it's no good trying to add classes to it. 该标记不是DOM对象,因此尝试向其添加类不是很好。

If you're trying to style the icon, you could use 如果您尝试设置图标的样式,则可以使用

function onClick(e) {

e.layer._icon.classList.add('activemarker');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM