简体   繁体   English

JavaScript:元素定位

[英]JavaScript: Element positioning

How can we change a position of an element in JS from a function?我们如何从函数中更改 JS 中元素的位置? object.style.left = "distance"; object.style.left = "距离"; --> It won't work with returned value, but with right format (12%, 12px). -->它不适用于返回值,但使用正确的格式 (12%, 12px)。

Code Example:代码示例:

/*Get random value*/
function move() {
    var distance = 10*Math.random();
    return distance;
}
/*Assign to the style.left property of an element*/
function assign() {
    var distance = move();
    object.style.left = "distance";
}```

"distance" should not be a string. “距离”不应是字符串。 It's a variable.这是一个变量。 Try:尝试:

object.style.left = distance;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM