如何通过javascript更改标题的边距

Ash*_*utt 0 html javascript

我想通过 javascript 更改我的 h1 边距,但它不起作用

我的代码:

<script>
  document.getElementById("h1").style.marginLeft = "250px";
</script>

<h1 id="h1" style="position: absolute; text-align: center; top: 100px;  color: #E95928;  font-family:Helvetica; font-weight: bold; font-size:6vw; ">Shop till<br>you drop</h1>
Run Code Online (Sandbox Code Playgroud)

ish*_*ood 5

您可以看到,如果您的函数放在它作用的元素之后,它确实可以工作。

h1 {
  position: absolute;
  text-align: center;
  top: 10px;
  color: #E95928;
  font-family: Helvetica;
  font-weight: bold;
  font-size: 6vw;
}
Run Code Online (Sandbox Code Playgroud)
<h1 id="h1">Shop 'til<br>you drop</h1>

<script>
  document.getElementById("h1").style.marginLeft = "250px";
</script>
Run Code Online (Sandbox Code Playgroud)