Javascript 如何重置多边形上的样式?

Javascript 如何重置多边形上的样式?,javascript,polygon,leaflet,Javascript,Polygon,Leaflet,我无法在传单多边形上重置样式。setStyle在悬停时工作正常,但是在我停止悬停时重置它不起作用。我得到了未捕获的TypeError:Object[Object Object]没有方法“resetStyle”。我理解这个错误的含义,但我不知道如何正确地做到这一点 提前谢谢 $.getJSON('geoJSON.json', function (json) { L.geoJson(json, { ... onEachFeature: function (fe

我无法在传单多边形上重置样式。
setStyle
在悬停时工作正常,但是在我停止悬停时重置它不起作用。我得到了未捕获的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);
});

上面的代码失败,因为可以在geojson层中找到resetStyle函数,而不是在“this”指向的层中

geojson层还需要为resetStyle的默认样式设置一个样式路径选项

$.getJSON('geoJSON.json', function (json) {
    var geojson = L.geoJson(json, {
        ...
        style: <default style here>
        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) {
                geojson.resetStyle();
            });
        }
    }).addTo(map);
});
$.getJSON('geoJSON.json',函数(json){
var geojson=L.geojson(json{
...
风格:
onEachFeature:功能(功能,图层){
var defaultStyle=layer.style;
层上('mouseover',函数(e){
这是我的风格({
颜色:“#2262CC”,
体重:3,
不透明度:0.6,
不透明度:0.65,
填充颜色:“#2262CC”
});
});
层上('mouseout',函数(e){
geojson.resetStyle();
});
}
}).addTo(地图);
});

不是我找不到处理样式的方法,而是resetStyle()对我不起作用。我想我找到了问题所在。这可能是上下文的问题。我来试一试。已经有两周没有做过这方面的工作了。=)