3

Using javascript - we can set the element relative position such as

object.style.position="absolute"||"fixed"||"relative"

But,on using the same console.log(object.style.position) - it does not return the position applied on the object - it returns NULL. Am i missing something here or is there another way to achieve what i'm trying to achieve??

4

2 回答 2

5

.style represents what's set on the element itself, much like the style attribute.

You could instead use getComputedStyle: http://jsfiddle.net/qAbTz/1/.

var div = document.getElementById("div");

console.log(div.style.position);              // "" (not null by the way)
console.log(getComputedStyle(div).position);​  // "fixed"
于 2012-06-06T20:08:19.783 回答
0

Note also (by the same logic presented by pimvdb), if you specify an initial position as part of the object's style, it is accessible by div.style.position.

<div id="div" style="position: absolute;"></div>

http://jsfiddle.net/qAbTz/4/

于 2012-06-06T20:11:05.813 回答