2 回答
TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
看來(lái)您正在嘗試將參數(shù) station_id 作為參數(shù)發(fā)送到函數(shù)刷新METAR 中,以便在單擊按鈕時(shí)發(fā)送請(qǐng)求。您的代碼中有兩個(gè)問(wèn)題。
<button onclick="refreshMETAR(this.innerHTML)">${station_id}</button>我認(rèn)為上面的語(yǔ)句中的這將引用窗口,而不是按鈕元素或 stationIdCell 元素。const refreshMETAR = () => { const stationElement = this.innerHTML //The value of the table cell should be read here您發(fā)送 (this.innerHtml) 作為 params ,但這不是在 param 中使用 a 的方法
請(qǐng)嘗試這個(gè):
stationIdCell.innerHTML = `<button onclick="refreshMETAR('${stationId}')">${station_id}</button>`
const refreshMETAR = (stationId) => {
const station = stationId
console.log(`entered: ${station}`)
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
事件委托典型案例
第一部分:
// 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
//...
點(diǎn)擊按鈕...
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}
// ...
}
添加回答
舉報(bào)
