2015-09-01 37 views
0

我有一些HTML元素在我的网页获取HTML元素顶部的值作为纯整数的JavaScript

<div id="element"></div> 

上,我想找出它top值存储在

document.querySelector('div#element').style.top; 

作为使用纯javascript的整数。

+0

如果您尚未设置_div的'style' object_的'top'属性(当在样式表中设置不计数)时,您将得到一个空字符串。你总是可以通过使用['getComputedStyle()'](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle)来获得一个值。或者你只是要求'parseInt()'? – Teemu

+0

没有样式属性以及顶级属性。所以它返回“” –

+1

你可以使用'document.querySelector('div#question-header')。offsetTop' –

回答

0

如果您希望top属性为Integer,则使用document.querySelector('div#element').offsetTop