din*_*uti 34 html javascript windows-store-apps
我正在通过javascript设置一个按钮,但按钮不显示文本.
有关如何解决它的任何建议?
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);
Run Code Online (Sandbox Code Playgroud)
谢谢!
Ste*_*vie 54
基本上,使用innerHTML而不是value,因为你要附加的'button'类型在其innerHTML中设置它的值.
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);
Run Code Online (Sandbox Code Playgroud)
在DOM中看起来像这样:
<div id="divWrapper">
<button content="test content" class="btn">test value</button>
</div>
Run Code Online (Sandbox Code Playgroud)
演示: http ://jsfiddle.net/CuXHm/
Den*_*ret 13
a的值按钮元素是不显示的文本,相反会发生什么input
类型的按钮元素.
你可以这样做 :
b.appendChild(document.createTextNode('test value'));
Run Code Online (Sandbox Code Playgroud)
创建一个文本节点并将其附加到按钮元素:
var t = document.createTextNode("test content");
b.appendChild(t);
Run Code Online (Sandbox Code Playgroud)
通过设置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);
Run Code Online (Sandbox Code Playgroud)