我无法在传单多边形上重置样式. 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);
});