我的問題是:如何制作表格單元格,例如,單擊時更改文本?我試過這個:
function randomFunction() {
document.getElementById("randomId").innerText = "random text"
}
<table>
<tr>
<td id="randomId" onclick="randomFunction()">random text</td>
</tr>
</table>
但它對整行執行 onclick 屬性。
是否可以僅在單擊單元格時觸發該功能?
uj5u.com熱心網友回復:
是的,有可能。您已將背景關系傳遞給函式。我在你的桌子上放了更多的單元格。
function randomFunction(e){
e.innerText = "HuhU"
// document.getElementById("randomId").innerText = "random text!"
}
td {
background: green;
color:white;
}
<table>
<tr>
<td id="randomId" onclick="randomFunction(this)">random text</td>
<td id="randomId" onclick="randomFunction(this)">random text</td>
<td id="randomId" onclick="randomFunction(this)">random text</td>
</tr>
</table>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/346982.html
標籤:javascript html
上一篇:對物件進行排序并洗掉重復項
下一篇:在p5.js中洗掉順序重復的幀
