gpt4 book ai didi

javascript - DOM 元素的 margin-left 不起作用

转载 作者:行者123 更新时间:2023-11-30 11:31:56 25 4
gpt4 key购买 nike

我尝试创建一个动态 P 元素,其 margin-left 为 33px。但它不起作用。但是当不使用左边(边距)并且使用静态 html p 元素时,同样可以正常工作。为什么会这样?我已尝试使用 IE11 和 Chrome 版本 60.0.3112.113。

<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
<meta charset="utf-8">
</head>
<body>
<div id="screenv" style="border:1px solid black;margin: 5px;padding: 5px;width: 300px;height: 300px;"></div></br>
<input id="tb" type="text" style="border:1px solid black;margin: 5px;padding: 5px;width: 300px;" placeholder="Enter your text"></input>
<button onclick="submit()">Submit</button>
<p style="margin-left:33px;">hello</p>//css works fine
<script type="text/javascript">

var you="",screen="",msg="",c_time="",val="",d,h,m,line;
function submit() {

val=document.getElementById("tb").value;
screen=document.getElementById('screenv');

you=document.createElement("div");
you.innerHTML="You:";

screen.appendChild(you);


c_time=document.createElement("p");
d = new Date();
h = d.getHours();
m = d.getMinutes();
c_time.innerHTML = h+":"+m;
c_time.style.display="inline";
c_time.style.color="grey";

msg=document.createElement("p");
msg.innerHTML=val;
msg.style.margin-left="33px";//not working


line=document.createElement("br");

you.appendChild(c_time);
you.appendChild(line);
you.appendChild(msg);
document.getElementById("tb").value="";
}


</script>

</body>
</html>

最佳答案

msg.style.margin-left="33px"; 将被解释为 as subtract operator由解析器。因此,所有带有破折号的 CSS 属性都写在 camelCase 中。在 Javascript 中。

关于javascript - DOM 元素的 margin-left 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45916799/

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