2 回答

TA貢獻1725條經(jīng)驗 獲得超8個贊
看來您正在嘗試將參數(shù) station_id 作為參數(shù)發(fā)送到函數(shù)刷新METAR 中,以便在單擊按鈕時發(fā)送請求。您的代碼中有兩個問題。
<button onclick="refreshMETAR(this.innerHTML)">${station_id}</button>
我認為上面的語句中的這將引用窗口,而不是按鈕元素或 stationIdCell 元素。const refreshMETAR = () => { const stationElement = this.innerHTML //The value of the table cell should be read here
您發(fā)送 (this.innerHtml) 作為 params ,但這不是在 param 中使用 a 的方法
請嘗試這個:
stationIdCell.innerHTML = `<button onclick="refreshMETAR('${stationId}')">${station_id}</button>`
const refreshMETAR = (stationId) => {
const station = stationId
console.log(`entered: ${station}`)

TA貢獻1848條經(jīng)驗 獲得超2個贊
事件委托典型案例
第一部分:
// GLOBAL
const metarTableElement = document.querySelector('#metar_table')
document.addEventListener('click', refreshMETAR )
//...
let rowCount = metarTableElement.rows.length
if (rowCount > 6)
{
metarTableElement.deleteRow( --rowCount )
}
else
{
let row = metarTableElement.insertRow()
row.insertCell().innerHTML = `<button class="cl_METAR">${station_id}</button>`
row.insertCell().textContent = latitude
row.insertCell().textContent = longitude
row.insertCell().textContent = raw_metar
//...
點擊按鈕...
function refreshMETAR(evt)
{
if (!evt.target.matches('button.cl_METAR')) return // reject clicks from somewhere else
const station = evt.target.textContent // === value in ${station_id}
// ...
}
添加回答
舉報