编程语言
首页 > 编程语言> > 通过javascript设置按钮文本

通过javascript设置按钮文本

作者:互联网

参见英文答案 > Setting a button’s value using javascript                                    4个
我正在通过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);

谢谢!

解决方法:

基本上,使用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);

在DOM中看起来像这样:

<div id="divWrapper">
    <button content="test content" class="btn">test value</button>
</div>

演示:http://jsfiddle.net/CuXHm/

标签:html,javascript,windows-store-apps
来源: https://codeday.me/bug/20191005/1856280.html