【问题标题】:Can't close custom popup at fixed position无法在固定位置关闭自定义弹出窗口
【发布时间】:2019-05-24 07:06:03
【问题描述】:

这是一个 javascript 初学者问题。 我在固定位置(传单)创建了一个自定义弹出窗口。单击打开弹出窗口的标记后,我无法通过单击关闭按钮来关闭它。虽然我可以单击不同的标记,但弹出包装仍然打开,显示附加到每个不同标记的内容。因此,通过单击标记会更改弹出窗口的内容,但无法通过单击关闭按钮关闭弹出窗口。
我尝试了一个事件监听器。 我需要完成这项工作的那段代码。 任何帮助将不胜感激。

// Adds custom marker

var redFlag = L.icon({
    iconUrl: 'images/mapmarker2.png',
    iconSize: [34, 34],
    iconAnchor: [17,34]
});

// Adds markers and popup
// geoJSON file stored in 'art' variable

const myLayer = L.geoJSON(art, {
    pointToLayer: function (feature, latlng) {
          return L.marker(latlng, {icon: redFlag});

},
onEachFeature: function ( feature, layer) {
    layer.on('click', function(e){

// popup content

   var getWrap = document.getElementById('wrapper');
        var wrap = getWrap.appendChild(document.createElement('div'));
        wrap.className = 'wrapper';
        wrap.innerHTML =  
           `<div class="close">X</div>`+ 
           `<div class="popUpContent" style="background-color:#e8f4ff">`+
           `<div class='pic'><img src = 
            "images/${feature.properties.image}"></div>`+ 
           `<div class="puName"><span 
            class="puName">${feature.properties.name}</span><br>`+
           `<span class="puTitle">"${feature.properties.title}"</span><br>`+ 
           `<div class="extra3">${feature.properties.extra}</div></div>`+ 
           `</div>`;

    if(!feature.properties.title){

        wrap.innerHTML =  
            `<div class="close">X</div>`+
            `<div class="popUpContent" style="background-color:#e8f4ff">` + 
             `<div class='pic'><img src = 
             "images/${feature.properties.image}"></div>`+ 
             `<div class="puName"><span 
             class="puName">${feature.properties.name}</span><br>`+ 
             `<div class="extra3">${feature.properties.extra}</div></div>`+ 
             `</div>`;
         }

// Add eventlistener to the close button

document.querySelector('.close').addEventListener( 'click', closePopup);
      function closePopup(e){

        if(event.target.matches('.close')){
            document.querySelector(".wrapper").style.display='none'
        }else if(document.querySelector(".wrapper").style.display='block'){
            document.querySelector(".wrapper").style.display='none';
           }
         }

       });
    }

});

mymap.addLayer(myLayer)

【问题讨论】:

  • 对于document.getElementById("wrapper").style.display === 'block',您不需要3x = 并且您的else 条件正在为wrapper 执行相同的样式,因此该if 条件的结果是没有意义的。您还复制了我不推荐的wrapper id。
  • 感谢 NewToJS,我更改了那段代码,但仍然遇到同样的问题。
  • 您仍在创建 wrapper 的重复 ID。 ID 应该是唯一的。在document.getElementById("wrapper").innerHTML 中,您还添加了具有相同iddiv => &lt;div id="wrapper" style="background-color:white;"&gt;

标签: javascript popup leaflet


【解决方案1】:

您在创建弹出窗口之前为close 添加事件侦听器。 您应该在onEachFeature 函数中的layer.on('click', function(e){... 末尾添加此侦听器。

为确保监听器只添加了事件,请在添加事件之前使用removeEventListener

【讨论】:

    【解决方案2】:

    事件监听器不起作用,因为弹出窗口是动态创建的。见Attach event to dynamic elements in javascript

    另外,我一直在做的就是在 HTML 中包含弹出窗口。但是将 CSS 显示属性设置为 display: none。当您需要显示弹出集时 以block 为例。

    这样你也可以在页面加载时添加事件监听器,因为弹出窗口不是动态创建的。只是隐藏,直到你需要它。

    【讨论】:

    • 仍然无法按我想要的方式工作。我可以打开一个弹出窗口,关闭它,打开另一个,然后它就卡住了。包装器保持打开状态并显示附加到各个标记的内容。猜猜我得按照leaflet内置的openpopup/closepopup来做
    【解决方案3】:

    我找到了答案。它是您构建动态生成的 DOM 元素的方式, 在这种情况下是弹出窗口。在 div 中创建一个 id="popup" 的空 div id="mapid" 在您的 HTML 文件中。其余部分是动态生成的。的 if 语句 事件监听器仅在条件为真时才需要该代码块。

    const element = document.getElementById('popup');
    
          element.innerHTML =
                `<div class= "wrapper" style="background-color:#fff">`+
                `<div class="close">X</div>`+ 
                `<div class="popUpContent" style="background-color:#e8f4ff">` +
                `<div class='pic'><img src = "images/${feature.properties.image}"></div> 
                 <p>`+ 
                `<div class="puName"><span class="puName">${feature.properties.name} 
                  </span><br>`+
                `<span class="puTitle">"${feature.properties.title}"</span><br><p>` + 
                `<div class="extra3">${feature.properties.extra}</div></div>`+ 
                `</div></div>`;
    
      // the eventListener
    
                document.getElementById('popup').addEventListener( 'click', closePopup);
                        function closePopup(e){
                    if(document.querySelector('.wrapper').style.display = 'block'){
                       document.querySelector('.wrapper').style.display='none';
                    }
                 }                 
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2021-08-29
      • 2018-04-03
      • 1970-01-01
      • 2014-01-19
      • 2019-03-12
      • 1970-01-01
      • 2013-02-09
      • 2019-10-07
      相关资源
      最近更新 更多