【发布时间】:2018-06-10 06:52:03
【问题描述】:
我运行他跟随(使用leaflet map):
HTML
<svg>
<path class="Mexico leaflet-interactive fadeIn"></path>
</svg>
JS
function selectNation(e) {
if(e.target.classList.contains("fadeIn")) {
console.log("hello");
var countryName = e.target.feature.properties.name;
$("#myCountryName").attr("value", countryName);
submitSearchForm();
}
}
function onEachFeature(feature, layer) {
layer.on({
click: selectNation
});
}
但它给了我:
未捕获的类型错误:无法读取未定义的“包含”属性
我试过了:
if($(e.target).hasClass("fadeIn")) {...
但它什么也不做,在控制台中什么也没说。我认为使用e.target 存在一些问题,但这有效:var countryName = e.target.feature.properties.name;。
我确认fadeIn 在svg path 上有一个班级。
更新
添加更多上下文:
我首先运行地图:
var map = L.map('map').setView([45.4655171, 12.7700794], 2);
加载地图后,我要求通过 ajax 回调加载一些变量,并将这些变量设置为路径类。
$.fn.almComplete = function(alm){
var foundNations = $.unique(nationList.sort());
$("path").each(function() {
for (var i = 0; i < foundNations.length; i++) {
if ($(this).hasClass(foundNations[i])) {
$(this).addClass("fadeIn");
}
}
});
};
添加类后,每个path 现在都有fadeIn 作为class,当我点击path 和class 时,我会这样做:
function selectNation(e) {
if(e.target.classList.contains("fadeIn")) {
console.log("hello");
var countryName = e.target.feature.properties.name;
$("#myCountryName").attr("value", countryName);
submitSearchForm();
}
}
function onEachFeature(feature, layer) {
layer.on({
click: selectNation
});
}
但我明白了:
未捕获的类型错误:无法读取未定义的“包含”属性
我愿意:
if($(e.target).hasClass("fadeIn")) {...
我什么也得不到。
注意
有人指出,这个问题可能与this one 重复,但在这里我只是检查它是否有一个不尝试匹配任何内容的类。
【问题讨论】:
标签: javascript jquery leaflet