我看到您的所有链接都具有相同的id="tiendanombre"
属性 - 这是故意的吗(id 属性在页面上应该是唯一的)?
根据这个答案如何从地图外的链接打开传单标记弹出窗口?标记有一个openPopup()
我们需要调用的方法
在您的 HTML 模板中,有没有一种方法可以识别diet
链接所指的数组中的哪个条目?可能是从同一个数组生成的 HTML 吗?
例如,如果我们可以在 HTML 中获取一个标识符
<a href="#" class="icon-link" data-diet-id="{Identifier that also appears in diet array}">Open in map</a>
然后我们可以在您forEach
制作标记后在您的循环中查找该链接
const marker = new L.marker(diet[i][0], diet[i][1]).bindPopup(etc etc);
// We need to find the link that corresponds to this marker.
// if the HTML was generated from the same diet array this will be quite simple,
// because it will be in the same position in the list of '.tiendaNombre a.icon-link' elements
// const link = document.querySelectorAll('.tiendaNombre a.icon-link')[i]
// otherwise if we have added an identifier to our HTML then we will need to find our link using that
const selector = `a[data-diet-id="${diet[i][IndexOfTheIdentifierField]}"]`;
const link = document.querySelector(selector);
// once we have found our link we just need to tell it what to do when it gets clicked
if(link) {
link.addEventListener('click', ()=>{
marker.openPopup();
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句