我的 JavaScript 中有这段代码。它创建按钮并具有 onclick 功能,但 .value 不起作用
var button = document.createElement("button");
button.value = "+";
button.onclick = addBox;
document.getElementById("section").appendChild(button);
最佳答案
不是value
但是textContent
。如果使用value,则需要使用<input type="button" />
。 Button
的文本位于 textContent
.
为什么textContent
而不是innerHTML
,因为textContent
速度更快,它不会尝试解析为 html
.
var button = document.createElement("button");
button.textContent = "+";
document.getElementById("section").appendChild(button);
<div id="section">
</div>
对于使用 type=button 输入
var button = document.createElement("input");
button.type = "button"
button.value = "+";
document.getElementById("section").appendChild(button);
<div id="section">
</div>
关于javascript - Button.value 没有更改按钮的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42162120/