Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript移动整个对象组_Javascript_Html_Css - Fatal编程技术网

如何使用Javascript移动整个对象组

如何使用Javascript移动整个对象组,javascript,html,css,Javascript,Html,Css,我正在尝试将div id“tile”中包含的所有内容移动到web浏览器的中心。默认情况下,CSS未定义,并显示在浏览器的左侧。我想做的是在单击按钮“move”时使用javascript将整个div移动到中心 html显示在正下方,尝试(但不工作)的javascript显示在下方 html 编辑:如何将div移动到特定位置?CSS中没有“align”属性。最接近的是,但您可能希望使用CSS声明margin:0 auto,它将整个移动到页面的中心。所以你想要: document.getElement

我正在尝试将div id“tile”中包含的所有内容移动到web浏览器的中心。默认情况下,CSS未定义,并显示在浏览器的左侧。我想做的是在单击按钮“move”时使用javascript将整个div移动到中心

html显示在正下方,尝试(但不工作)的javascript显示在下方

html

编辑:如何将div移动到特定位置?

CSS中没有“align”属性。最接近的是,但您可能希望使用CSS声明
margin:0 auto
,它将整个
移动到页面的中心。所以你想要:

document.getElementById("tile").style.margin="0 auto";

确保
平铺
具有指定的宽度。

只需CSS即可:

<div id="tile" style='margin:0 auto;width:300px'>
    ...
</div>
它必须定义一个
位置
,通常是
绝对
相对

同样,这可以而且通常应该通过CSS实现:

#tile { position:absolute; left:100px; top:100px }

如何将div放置在特定位置(顶部:100px,左侧:100px)?使用相同的方法,但需要使用三次。第一次,将末端替换为
style.position=“absolute”
,第二次使用
style.top=“100px;”
,第三次使用
style.left=“100px;”
。我忘了“绝对声明”。非常感谢!
<div id="tile" style='margin:0 auto;width:300px'>
    ...
</div>
<div id='container' style='text-align:center'>
  <div id='tile' style='width:300px'>
    ...
  </div>
</div>
document.getElementById('tile').style.position = "absolute";
document.getElementById('tile').style.left = "100px";
document.getElementById('tile').style.top = "100px";
#tile { position:absolute; left:100px; top:100px }