javascript – 如何在多边形上重置样式?

我无法在传单多边形上重置样式. setStyle在悬停时工作得很好,但是当我停止悬停时重置它不起作用.我得到Uncaught TypeError:Object [object Object]没有方法’resetStyle’.我明白这个错误意味着什么,但我无法弄清楚如何正确地做到这一点.

提前致谢.

$.getJSON('geoJSON.json', function (json) {
    L.geoJson(json, {
        ...
        onEachFeature: function (feature, layer) {
            var defaultStyle = layer.style;

            layer.on('mouseover', function (e) {
                this.setStyle({
                    color: '#2262CC',
                    weight: 3,
                    opacity: 0.6,
                    fillOpacity: 0.65,
                    fillColor: '#2262CC'
                });
            });
            layer.on('mouseout', function (e) {
                this.resetStyle();
            });
        }
    }).addTo(map);
});

解决方法:

这可能是一个上下文问题:

$.getJSON('geoJSON.json', function (json) {
    L.geoJson(json, {
        ...
        onEachFeature: function (feature, layer) {
            var defaultStyle = layer.style,
                that = this;//NEW

            layer.on('mouseover', function (e) {
                this.setStyle({
                    color: '#2262CC',
                    weight: 3,
                    opacity: 0.6,
                    fillOpacity: 0.65,
                    fillColor: '#2262CC'
                });
            });
            layer.on('mouseout', function (e) {
                that.resetStyle(); //NEW
            });
        }
    }).addTo(map);
});
上一篇:leaflet地图资源整理


下一篇:javascript – 在传单中与Lat Long坐标