前台有一个<div id="content">div里面的内容不是固定的,所以高度也不是固定的,在DIV的样式里面也没有写height属性;然而在JavaScript里面怎么取它的当前高度?我试过用JQuery的$("#content").css("height")去取,但是取出来的是auto;也试过用document.getElementById("content").style.height去取,取出来是一个空字符串;我的问题就是:怎么取它当前的具体高度? 比如400px.
调试欢乐多
$("#content").innerHeight();//元素内部区域高度,忽略padding、border
$("#content").outerHeight();//忽略边框
$("#content").outerHeight(true);//包含边框高度
来自《锋利的query》90页,希望对LZ有帮助
style可以获取你设置的值
想要获取计算后的值,在IE中使用currentStyle,其他浏览器里用document.getComputedStyle
自己写方法的话,是这样的:
function getStyle(){
return document.defaultView?document.defaultView.getComputedStyle(obj,null).getPropertyValue(style):obj.currentStyle[style.replace(/-[a-z]/g,function(){return arguments[0].charAt(1).toUpperCase();})];
}如果是用Jquery中的方法,应该是:
$("#content").height();
结果还是 auto
囧.....
window.document.getElementById("y1yx2").clientHeight
kuan=window.document.getElementById("y1yx2").clientHeight
if (kuan<296){window.document.getElementById("y1yx2").style.height=296}
}