gpt4 book ai didi

javascript - 为什么用 javascript 创建的文本框显示不同?

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:22 24 4
gpt4 key购买 nike

为什么在这段代码中,当我按下 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>

更新:
我在@Bergi 的 fiddle 中看到的:

enter image description here

最佳答案

style 属性不是字符串。它是一个对象,其中每个 CSS 属性都表示为一个 DOM 属性。

它还有the cssText property其中包括所有规则。

txt.style.cssText = "width: 30px; padding: 2px; border: 1px solid black";

关于javascript - 为什么用 javascript 创建的文本框显示不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16904967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com