我尝试复制并粘贴错误并找到了资源,但我实际上无法理解它是什么。 我遇到了这个弹出问题,请务必帮我解决。 我要附加代码段以及控制台日志中弹出的错误。
代码段:
for (var i=0; i<quakePoints.length; i++) {
var lon = quakePoints[i][1];
var lat = quakePoints[i][0];
var popupText = quakePoints[i][2];
var markerLocation = new L.LatLng(lat, lon);
var marker = new L.marker(markerLocation);
marker.bindPopup(popupText).addTo(map);
}
错误:
leaflet.js:7 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at e._updateContent (leaflet.js:7)
at e.update (leaflet.js:7)
at e.onAdd (leaflet.js:7)
at e._layerAdd (leaflet.js:6)
at e.addLayer (leaflet.js:6)
at e.openPopup (leaflet.js:7)
at e.openPopup (leaflet.js:7)
at e.togglePopup (leaflet.js:7)
at e.fireEvent (leaflet.js:6)
at e._onMouseClick (leaflet.js:7)
错误-使用leaflet-src.js
leaflet-src.js:4046 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
at NewClass._updateContent (leaflet-src.js:4046)
at NewClass.update (leaflet-src.js:3974)
at NewClass.onAdd (leaflet-src.js:3899)
at NewClass._layerAdd (leaflet-src.js:2265)
at NewClass.addLayer (leaflet-src.js:1723)
at NewClass.openPopup (leaflet-src.js:4172)
at NewClass.openPopup (leaflet-src.js:4197)
at NewClass.togglePopup (leaflet-src.js:4215)
at NewClass.fireEvent (leaflet-src.js:466)
at NewClass._onMouseClick (leaflet-src.js:3738)
编辑,附加示例数据
var quakePoints = [
[17.123184,79.208824,1.7345],
[19.123184,76.208824,2.7345],
[18.123184,69.208824,2.7345],
[21.123184,70.208824,3.7345],
[23.123184,72.208824,2.6645],
[22.123184,77.208824,1.2245],
[24.123184,85.208824,2.7345],
[18.123184,78.208824,1.7345],
[11.123184,89.208824,1.7345]];
答案 0 :(得分:3)
根据您的另一个问题,quakePoints
数组如下:
var quakePoints = [
[17.123184,79.208824,1.7345],
...
];
这意味着popupText
在这里...
var popupText = quakePoints[i][2];
...获取值1.7345
,但获取为 Number ,而不是 String 。
后来,Leaflet正在运行this piece of code,内部参数content
设置为 Number 1.7345
:
if (typeof content === 'string') {
node.innerHTML = content;
} else {
// snip
node.appendChild(content);
}
该参数不是一个 String ,因此它将尝试将其作为HTMLElement
处理。
请仔细阅读definition of the bindPopup
method from the Leaflet API reference:
bindPopup(<String|HTMLElement|Function|Popup> content, <Popup options> options?)
使用传递的内容将弹出窗口绑定到该层,并设置必要的事件侦听器。如果传递了
Function
,它将接收该图层作为第一个参数,并应返回String
或HTMLElement
。
请注意,bindPopup
的第一个参数必须是String
,HTMLElement
,返回Function
或{{ 1}}或String
的实例。您正在传递HTMLElement
,但这些都不是,所以您的问题实际上是GIGO的情况。
这里的方法是确保您传递的是Popup
:
例如与String()
:
Number
或例如与Number.toString()
:
String