一个JavaScript获取元素当前高度的实例
更新时间:2014年10月29日 17:27:26 投稿:whsnow
这篇文章主要为大家介绍了一个JavaScript获取元素当前高度的实例,比较实用,建议新手朋友们可以看看
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>每天一个JavaScript实例-获取元素当前高度</title>
<style>
#date{width:90%;height:25%;padding:10px;background: red;}
</style>
<script>
window.onload = function(){
var height = getHeight();
console.log(height);
}
function getHeight(){
var height = 0;
var div = document.getElementById("date").getBoundingClientRect();
if(div.height){
height = div.height;
}else{
height = div.bottom - div.top;
}
return height;
}
</script>
</head>
<body>
<div style="width:1000px;height:800px;">
<div id = "date">
</div>
</div>
</body>
</html>
相关文章
javascript contains和compareDocumentPosition 方法来确定是否HTML节点间的关
一个很棒的 blog 文章,是 PPK 两年前写的,文章中解释了 contains() 和 compareDocumentPosition() 方法运行在他们各自的浏览器上。2010-02-02
用JavaScript 判断用户使用的是 IE6 还是 IE7
判断IE浏览器的脚本,方便根据浏览器不懂,支持不同的代码的分别调用。2008-01-01


最新评论