方法一:使用jquery $("#id").height();
方法二:使用dom
document.getElementById("id").style.height (需要这样设置才能获取到高度,<div id="idName" style="height:300px;">)
document.getElementById("id").offsetHeight
document.getElementById("id").clientHeight
方法三:window.getComputedStyle(document.getElementById("id")).height;
(具体内容参考:http://www.zhangxinxu.com/wordpress/2012/05/getcomputedstyle-js-getpropertyvalue-currentstyle/)
getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式声明对象([object CSSStyleDeclaration]),只读。
语法如下:
var style = window.getComputedStyle("元素", "伪类");
例如:
var dom = document.getElementById("test"),
var style = window.getComputedStyle(dom , ":after");
就两个参数,大家都懂中文的,没什么好说的。只是额外提示下:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null),不过现在嘛,不是必需参数了。