Cra*_*lus 0 html javascript css
为什么在此代码中我按下时创建的文本框push与已显示的文本框不完全相同?
<html>
<body id="bd">
<input type="text" style="width: 30px; padding: 2px; border: 1px solid black"/>
<input type="submit" value="Push" onclick="test()"/>
<script type="text/javascript">
function test() {
var txt = document.createElement('input');
txt.type = 'text';
txt.style = "width: 30px; padding: 2px; border: 1px solid black";
document.getElementById('bd').appendChild(txt);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
更新:
我在@Bergi的小提琴中看到的:

该style属性不是字符串.它是一个对象,其中每个CSS属性都表示为DOM属性.
它还拥有包含所有规则的cssText财产.
txt.style.cssText = "width: 30px; padding: 2px; border: 1px solid black";
Run Code Online (Sandbox Code Playgroud)