我有一个 html 按钮,如下所示:
<button onclick="func()" id="accountDetails" runat="server"</button>
我已经花了很多时间摸索如何通过单击按钮来更改按钮的文本,我放置了以下执行onclick的func()强>事件(html):
<script type="text/javascript">
function func() {
document.getElementById('accountDetails').textContent = 'server';
}
</script>
但结果是按钮上的文本仅更改了一秒钟(我的意思是当我单击按钮时),然后旧文本再次显示在按钮上。
最佳答案
您可以遵循此html
和脚本
。
使用输入而不是按钮。
<input onclick="func()" id="accountDetails" type="button" value="click"></input>
而不是
<button onclick="func()" id="accountDetails" runat="server"</button>
然后 document.getElementById('accountDetails')
需要设置 value
而不是 textContent
function func() {
document.getElementById('accountDetails').value = 'server';
}
<input onclick="func()" id="accountDetails" type="button" value="click"></input>
关于javascript - 通过单击 html 按钮来更改其文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406691/