[英]How do I make an event Listener (onclick) for a single Table Cell
my question is: How can I make a table cell, for example, change text on click?我的问题是:如何制作表格单元格,例如,单击时更改文本? I tried this:
我试过这个:
function randomFunction() { document.getElementById("randomId").innerText = "random text" }
<table> <tr> <td id="randomId" onclick="randomFunction()">random text</td> </tr> </table>
but it does the onclick attribute for the entire row.但它对整行执行 onclick 属性。
Is it possible to trigger the function only when I click on the cell ?是否可以仅在单击单元格时触发该功能?
Yes it possible.是的,有可能。 you have pass the context to the function.
您已将上下文传递给函数。 I put more cells in your table.
我在你的桌子上放了更多的单元格。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.