簡體   English   中英

使用 Javascript 變量中的值定位元素

[英]Positioning an element using values from Javascript variables

如何使用 Javascript 變量中的xy坐標值來 position 一個元素,例如div標簽?

If you're trying to position the object in absolute x,y coordinates on the page, just set the object to position: absolute and then set the values of top and left to your x and y.

HTML:

<div id="square" style="background-color: #777; height: 100px; width: 100px;"></div>

代碼:

var s = document.getElementById("square");
s.style.position = "absolute";
var x = 100, y = 200;
s.style.left = x + "px";
s.style.top = y + "px";

你可以在這里看到一個工作代碼示例: http://jsfiddle.net/jfriend00/hhpDQ/

我不確定你可以設置坐標,但你可以設置邊距。 就像是

$("#divid").css("margin-left" : "200px" , "margin-up" : "200px");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM