使用 getElementById 提取 CSS 数据

Pulling CSS data using getElementById

本文关键字:数据 CSS 提取 getElementById 使用      更新时间:2023-09-26

我正在尝试从元素中提取CSS数据,但由于某种原因我碰壁了。我一直盯着并试图调整同一行代码一个小时,但我不断得到未定义或 NaN。

这是缩写的js:

var newVar = document.getElementById("cssItem").style.top;

该 css:

#cssItem {
position:   absolute;
top:        100px;
left:       100px;
width:      600px;
height:     400px; 
}

同样,每次我登录 newVar 时,它都会返回未定义或 NaN。

Node.style仅适用于内联样式。如果要获取渲染样式,请使用以下命令:

var newVar = window.getComputedStyle( document.getElementById("cssItem") ).top;

这是小提琴:http://jsfiddle.net/q93B6/