This question already has an answer here:
这个问题在这里已有答案:
- Setting a button's value using javascript 4 answers
- 使用javascript 4答案设置按钮的值
I am setting up a button via javascript, but the button shows not the text.
我正在通过javascript设置一个按钮,但按钮不显示文本。
Any recommendation on how to fix it?
关于如何修复它的任何建议?
var b = document.createElement('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.value = 'test value';
var wrapper = document.getElementById(divWrapper);
wrapper.appendChild(b);
Thanks!
谢谢!
4 个解决方案
#1
30
Basically, use innerHTML instead of value, because the 'button' type you are appending sets it's value in its innerHTML.
基本上,使用innerHTML而不是value,因为你要附加的'button'类型在innerHTML中设置它的值。
JS:
JS:
var b = document.createElement('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.innerHTML = 'test value';
var wrapper = document.getElementById("divWrapper");
wrapper.appendChild(b);
Looks like this in the DOM:
在DOM中看起来像这样:
<div id="divWrapper">
<button content="test content" class="btn">test value</button>
</div>
Demo: http://jsfiddle.net/CuXHm/
演示:http://jsfiddle.net/CuXHm/
#2
6
The value of a button element isn't the displayed text, contrary to what happens to input
elements of type button.
button元素的值不是显示的文本,与类型按钮的输入元素发生的情况相反。
You can do this :
你可以这样做 :
b.appendChild(document.createTextNode('test value'));
示范
#3
4
Create a text node and append it to the button element:
创建一个文本节点并将其附加到button元素:
var t = document.createTextNode("test content");
b.appendChild(t);
#4
1
Set the text of the button by setting the innerHTML
通过设置innerHTML来设置按钮的文本
var b = document.createElement('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.innerHTML = 'test value';
var wrapper = document.getElementById('divWrapper');
wrapper.appendChild(b);
http://jsfiddle.net/jUVpE/
#1
30
Basically, use innerHTML instead of value, because the 'button' type you are appending sets it's value in its innerHTML.
基本上,使用innerHTML而不是value,因为你要附加的'button'类型在innerHTML中设置它的值。
JS:
JS:
var b = document.createElement('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.innerHTML = 'test value';
var wrapper = document.getElementById("divWrapper");
wrapper.appendChild(b);
Looks like this in the DOM:
在DOM中看起来像这样:
<div id="divWrapper">
<button content="test content" class="btn">test value</button>
</div>
Demo: http://jsfiddle.net/CuXHm/
演示:http://jsfiddle.net/CuXHm/
#2
6
The value of a button element isn't the displayed text, contrary to what happens to input
elements of type button.
button元素的值不是显示的文本,与类型按钮的输入元素发生的情况相反。
You can do this :
你可以这样做 :
b.appendChild(document.createTextNode('test value'));
示范
#3
4
Create a text node and append it to the button element:
创建一个文本节点并将其附加到button元素:
var t = document.createTextNode("test content");
b.appendChild(t);
#4
1
Set the text of the button by setting the innerHTML
通过设置innerHTML来设置按钮的文本
var b = document.createElement('button');
b.setAttribute('content', 'test content');
b.setAttribute('class', 'btn');
b.innerHTML = 'test value';
var wrapper = document.getElementById('divWrapper');
wrapper.appendChild(b);
http://jsfiddle.net/jUVpE/