我正在嘗試在單擊元素時呼叫方法。HTML 存盤在一個變數中。如下所示:
var cont = '<div onClick="clickPoly(' index ')" style="width:100px;" >View More</div>';
const clickPoly = (index) => {
var square = coordinates[index];
if(square != undefined){
square.dispatchEvent('click');
}
}
clickPoly() 應該被稱為上面 html 的 onclick。但是當我點擊“查看更多”時,它顯示“clickPoly 未定義”。
基本上,我在谷歌地圖上顯示多個多邊形,在每個多邊形的懸停上我顯示資訊視窗。在 infoWindow 中,我需要顯示“查看更多”按鈕并顯示相關內容。
你可以在這里看到完整的代碼
uj5u.com熱心網友回復:
clickPoly需要在視窗上定義。
window.clickPoly = (index) => {
var square = coordinates[index];
if (square != undefined) {
square.dispatchEvent("click");
}
};
或者,將 HTMLElement 傳遞給infowindow.setContent(element)并在組件中添加事件
var cont = '<div data-inx="' index '" onClick="clickPoly(' index ')" style="width:100px;" >View More</div>';
變成
const cont = document.createElement('div').
cont.classList.add('infocontent2');
cont.addEventListener(() => clickPoly(index));
// add aria-label
cont.style.width = '100px';
cont.innerText = 'View More'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/421271.html
標籤:
