Javascript 按钮不存在';API弹出窗口、传单传送机中的t触发器功能 功能添加弹出窗口(功能、图层){ 让popupContent=`

Javascript 按钮不存在';API弹出窗口、传单传送机中的t触发器功能 功能添加弹出窗口(功能、图层){ 让popupContent=`,javascript,leaflet,Javascript,Leaflet,${features.properties.NAAM} ${features.properties.TYPE} ${features.properties.OMSCHRIJVING} ${features.properties.POSTCODE}${features.properties.DISTRICT} Breng mij naar hier.` layer.bindPopup(弹出内容); } var routeControl=L.Routing.control({ 航路点:[ L.lat

${features.properties.NAAM}
${features.properties.TYPE}
${features.properties.OMSCHRIJVING}
${features.properties.POSTCODE}${features.properties.DISTRICT}
Breng mij naar hier.

` layer.bindPopup(弹出内容); } var routeControl=L.Routing.control({ 航路点:[ L.latLng(位置坐标纬度位置坐标经度), L.latLng(51.2194475,4.4024643) ], 鲁特惠莱德拉金:没错, 地理编码器:L.Control.geocoder.namignem() }).addTo(mymap); document.getElementById(“btn”).addEventListener('click',function()){ routeControl.拼接航路点(routeControl.getWaypoints().length-1,1,e.latlng); mymap.closePopup(); }); 我正在制作一个弹出窗口,其中包含从API获取的信息,在信息下方有一个按钮。我的期望是,当我按下按钮时,我的功能会工作,我在地图上的航路点会改变。据我所知,问题是我无法按住id=btn的按钮来执行我的功能


是否有办法使其工作?

当您初始化clicklistener时,您的按钮未加载到DOM中

打开弹出窗口后,必须设置clicklistener

function addPopup(features, layer) {
        let popupContent = `<p>${features.properties.NAAM}<br>
   ${features.properties.TYPE}<br>
   ${features.properties.OMSCHRIJVING}<br>
   ${features.properties.POSTCODE} ${features.properties.DISTRICT}<br>
    <button type="button" id="btn" >Breng mij naar hier.</button> </p>`
        layer.bindPopup(popupContent);
    }

var routeControl = L.Routing.control({
        waypoints: [
            L.latLng(location.coords.latitude, location.coords.longitude),
            L.latLng(51.2194475, 4.4024643)
        ],
        routeWhileDraggin: true,
        geocoder: L.Control.Geocoder.nominatim()
    }).addTo(mymap);


document.getElementById("btn").addEventListener('click', function(){
        routeControl.spliceWaypoints(routeControl.getWaypoints().length - 1, 1, e.latlng);
        mymap.closePopup();
    });
功能添加弹出窗口(功能、图层){
让popupContent=`${features.properties.NAAM}
${features.properties.TYPE}
${features.properties.OMSCHRIJVING}
${features.properties.POSTCODE}${features.properties.DISTRICT}
Breng mij naar hier.

` layer.bindPopup(弹出内容); 层上('popupopen',函数(e){ document.getElementById(“btn”).addEventListener('click',function()){ routeControl.拼接航路点(routeControl.getWaypoints().length-1,1,e.popup.\u source.getLatLng()); mymap.closePopup(); }); } }
请检查答案!我最初遇到此错误:TypeError:无法读取null的属性'addEventListener'。使用代码后,我不再遇到此错误。但现在路由不起作用,当我检查(e.latlng)时,它未定义。如果我更新为此。getLatLng()我得到这个错误:Uncaught TypeError:this.getLatLng不是HTMLButtonElement的函数。如果我发现它,我会告诉你。我在开始时使用这个网站作为指南:ups抱歉,我更新了答案…使用:
e.popup.\u source.getLatLng()
function addPopup(features, layer) {
        let popupContent = `<p>${features.properties.NAAM}<br>
   ${features.properties.TYPE}<br>
   ${features.properties.OMSCHRIJVING}<br>
   ${features.properties.POSTCODE} ${features.properties.DISTRICT}<br>
    <button type="button" id="btn" >Breng mij naar hier.</button> </p>`
        layer.bindPopup(popupContent); 
        layer.on('popupopen',function(e){
           document.getElementById("btn").addEventListener('click', function(){
              routeControl.spliceWaypoints(routeControl.getWaypoints().length - 1, 1, e.popup._source.getLatLng());
              mymap.closePopup();
           });
        }
    }