javascript element.style.left 不工作

Vis*_*nan 6 html javascript jquery web

通过javascript设置元素顶部,左侧停止工作,以前它工作得很好。

ifrmObj.style.left = 100;
ifrmObj.style.top = 150;
Run Code Online (Sandbox Code Playgroud)

现在我必须添加一个带有数字的“px”。我所做的更改是导致此问题的原因

Roh*_*mar 8

首先使用位置然后左或顶部将工作,

句法:

object.style.position="static|absolute|fixed|relative|initial|inherit" 
Run Code Online (Sandbox Code Playgroud)

例如,

ifrmObj.style.position='absolute';//relative or fixed
Run Code Online (Sandbox Code Playgroud)

要在您的数字中添加 px,

对于静态数字

ifrmObj.style.left = '100px';
ifrmObj.style.top = '150px';
Run Code Online (Sandbox Code Playgroud)

对于变量数

var x=100,y=150;
ifrmObj.style.left = x+'px';
ifrmObj.style.top = y+'px';
Run Code Online (Sandbox Code Playgroud)

如果您正在使用 jquery,那么一次性使用css()

$(ifrmObj).css({'left':'100px','top':'150px','position':'absolute'});
Run Code Online (Sandbox Code Playgroud)