我正在尝试创建一个自定义按钮,以显示在生成动态链接(URL)的弹出窗口中。由于时间原因,我似乎无法通过 .setHTML 执行此操作,无法在运行时将按钮绑定到函数。所以我想我应该尝试新的 .setDOMContent
关于此功能如何工作的在线信息为零。我想知道是否有人有这样的示例,其中将按钮添加到弹出窗口中,可以运行函数并发送数据。
这是我对此进行的非常糟糕的尝试。
该函数创建弹出窗口
function GameObjectPopup(myObject) {
var features = map.queryRenderedFeatures(myObject.point, {
layers: ['seed']
});
if (!features.length) {
return;
}
var feature = features[0];
// Populate the popup and set its coordinates
// based on the feature found.
var popup = new mapboxgl.Popup()
.setLngLat(feature.geometry.coordinates)
.setHTML(ClickedGameObject(feature))
.setDOMContent(ClickedGameObject2(feature))
.addTo(map);
};
该函数通过 .setHTML 添加 html
function ClickedGameObject(feature){
console.log("clicked on button");
var html = '';
html += "<div id='mapboxgl-popup'>";
html += "<h2>" + feature.properties.title + "</h2>";
html += "<p>" + feature.properties.description + "</p>";
html += "<button class='content' id='btn-collectobj' value='Collect'>";
html += "</div>";
return html;
}
该函数想要通过.setDOMContent添加DOM内容
function ClickedGameObject2(feature){
document.getElementById('btn-collectobj').addEventListener('click', function()
{
console.log("clicked a button");
AddGameObjectToInventory(feature.geometry.coordinates);
});
}
我正在尝试将变量从 features.geometry.coordinates 传输到函数 AddGameObjectToInventory() 中
单击对象时出现的错误(以便生成弹出窗口)
Uncaught TypeError: Cannot read property 'addEventListener' of null