简体   繁体   English

单击时更改按钮类型上的文本

[英]Change text on a button type when clicked

I can't change the text of a button when the button type is button, but I can change it if the type is input当按钮类型为按钮时,我无法更改按钮的文本,但如果输入类型,我可以更改它

The code for the button is按钮的代码是

<button type='button'
        id='1'
        name='4'
        class='btn btn-select'>Text I want to change</button>

In the handler I am trying to change it like so:在处理程序中,我试图像这样改变它:

document.getElementById("1").value="New Button Text";

If I change it input type it works?如果我改变它的输入类型它有效吗? Why?为什么?

The label for a <input type="button"> is determined by the value attribute. <input type="button">的标签由value属性决定。 An <input type="submit"> gets its label and submitted data from the value attribute (they can't be different). <input type="submit">value属性获取它的标签和提交的数据(它们不能不同)。

The label for a <button> element is determined by its text content . <button>元素的标签由其文本内容决定。 The value attribute determines what data it will submit to the server (if it were a submit button) when clicked. value属性确定单击时将向服务器提交哪些数据(如果它是提交按钮)。 They can be different.它们可以不同。

 document.getElementById("1").textContent = "New Button Text";
 <button type='button' id='1' name='4' class='btn btn-select'>Text I want to change</button>

TRY:尝试:

document.getElementById("1").innerText="New Button Text";

you are use the button as a tag not as input field您将按钮用作标签而不是输入字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM