我试图从一个元素中提取 CSS 数据,但由于某种原因我碰壁了。一个小时以来,我一直盯着并尝试调整同一行代码,但我一直得到 undefined 或 NaN。
这是缩写的js:
var newVar = document.getElementById("cssItem").style.top;
CSS:
#cssItem {
position: absolute;
top: 100px;
left: 100px;
width: 600px;
height: 400px;
}
同样,每次我记录 newVar 时,它都会返回 undefined 或 NaN。
最佳答案
Node.style
仅适用于内联样式。如果你想获得呈现的样式,使用这个:
var newVar = window.getComputedStyle( document.getElementById("cssItem") ).top;
这是 fiddle :http://jsfiddle.net/q93B6/
关于javascript - 使用 getElementById 获取 CSS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639985/