1

I have the following code and it seems like the .style does not recognize my variable wid! what is wrong with it?

var wid = document.getElementById("bd").offsetWidth/2;
obj = document.getElementById('div1'); 
obj.style.left = wid.toString();

'bd' is the id of my body and 'div1' the id of the div i want to move. If I just use the following, it works fine:

obj.style.left = '10px';
4

2 回答 2

2

您不需要使用toString,只需附加px到数字:

var wid = document.getElementById("bd").offsetWidth/2;
obj = document.getElementById('div1'); 
obj.style.left = wid + 'px';
于 2013-10-30T00:16:35.803 回答
0

好的...我刚刚更改了我的代码:

obj.style.left = wid.toString();

至:

object.style.left = wid.toString()+'px';

它工作得很好

于 2013-10-30T00:16:29.400 回答