javascript style.top/left不能在div中工作

javascript style.top/left不能在div中工作,javascript,Javascript,我只是想移动一个div,修改this.elemento.style.top和this.elemento.style.left 如您所见,首先获取style.top和style.left的值,然后尝试在控制台上手动修改sttyle.top/left的值: >>> this.elemento.style.top "48px" >>> this.elemento.style.left "18px" >>> this.elemento.style.

我只是想移动一个div,修改this.elemento.style.top和this.elemento.style.left

如您所见,首先获取style.top和style.left的值,然后尝试在控制台上手动修改sttyle.top/left的值:

>>> this.elemento.style.top 
"48px"
>>> this.elemento.style.left
"18px"
>>> this.elemento.style.top = "500px";
"500px"
>>> this.elemento.style.left = "500px";
"500px"


当我使用this.elemento时,它只是hmtl中的一个div。这是一个例子:

我假设您试图给div一个绝对位置。为此,您需要给出其父容器的相对位置。 在你的CSS中添加这个

#parent{
  position: relative;
}
#child{
  position: absolute;
}
如果您喜欢在JS中执行此操作,请将其添加到您的JS文件中:

parent.elemento.style.position = relative; 
this.elemento.style.position = absolute;

style.position
?>>此.elemento.style.position为空。回答中提到,position应设置为其他值,然后设置为“static”,这是默认值: