嗨,我得到对象 HTMLButtonElement
由于var btn = document.createElement('button');
这是正常行为
但我怎样才能得到普通按钮作为图形而不是对象呢?
即<button type="button">
就像将对象转换为字符串,但在本例中 反对什么?
最佳答案
在dom中,Button存储为 HTMLButtonElement 的实例对象,这就是为什么当您尝试将其转换为字符串(调用 toString()
)时,您会得到 [object HTMLButtonElement]
。
由于您想将按钮添加到 View (dom 树)中,因此可以使用 appendChild() 将按钮实例附加到树中喜欢
var btn = document.createElement('button');
btn.innerText = 'this button';
document.getElementById('container').appendChild(btn);
document.getElementById('markup').innerText = btn.outerHTML;
<div id="container"></div>
<pre id="markup"></pre>
关于javascript - 如何将 HTMLButtonElement 对象转换为 html 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563108/