代码如下:
第一个div的overflow原先是hidden 通过js修改为scroll后再获得scrollHeight得到的值是108
第二个div的overflow原先就是scroll,通过js获得scrollHeight得到的值是252
为什么会不一样呢??求教,谢谢!
<html>
<head>
<title></title>
<script type="text/javascript">
function getHeight(id){
var obj = document.getElementById(id);
obj.style.overflow = "scroll";
alert(obj.scrollHeight);
obj.style.overflow = "hidden";
}
window.onload = function () {
getHeight("dh");
alert(document.getElementById('ds').scrollHeight)
}
</script>
<style type="text/css">
.divHidden{
width:100px;
height:100px;
overflow:hidden;
}
.divScroll{
width:100px;
height:100px;
overflow:scroll;
}
</style>
</head>
<body>
<div id="dh" class="divHidden">
第一行
dfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsgdfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsg
最后一行
</div>
<input type="button" value="getHiddenHeight" onclick="getHeight('dh')" />
</br>
<div id="ds" class="divScroll">
第一行
dfsadfadf
asdf
sdffsadfadf
asdf
sdf
dsfsd
dsfsd
hasdgjoijgdsgdfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsg
最后一行
</div>
<input type="button" value="getScrollHeight" onclick="alert(document.getElementById('ds').scrollHeight)" />
</body>
</html>
第一个div的overflow原先是hidden 通过js修改为scroll后再获得scrollHeight得到的值是108
第二个div的overflow原先就是scroll,通过js获得scrollHeight得到的值是252
为什么会不一样呢??求教,谢谢!
<html>
<head>
<title></title>
<script type="text/javascript">
function getHeight(id){
var obj = document.getElementById(id);
obj.style.overflow = "scroll";
alert(obj.scrollHeight);
obj.style.overflow = "hidden";
}
window.onload = function () {
getHeight("dh");
alert(document.getElementById('ds').scrollHeight)
}
</script>
<style type="text/css">
.divHidden{
width:100px;
height:100px;
overflow:hidden;
}
.divScroll{
width:100px;
height:100px;
overflow:scroll;
}
</style>
</head>
<body>
<div id="dh" class="divHidden">
第一行
dfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsgdfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsg
最后一行
</div>
<input type="button" value="getHiddenHeight" onclick="getHeight('dh')" />
</br>
<div id="ds" class="divScroll">
第一行
dfsadfadf
asdf
sdffsadfadf
asdf
sdf
dsfsd
dsfsd
hasdgjoijgdsgdfsadfadf
asdf
sdf
dsfsdfsadfadf
asdf
sdf
dsfsd
hasdgjoijgdsg
最后一行
</div>
<input type="button" value="getScrollHeight" onclick="alert(document.getElementById('ds').scrollHeight)" />
</body>
</html>
----------------------------------签----------名----------栏----------------------------------
https://developer.mozilla.org/en/DOM/element.scrollHeight
var obj = document.getElementById(id);
obj.style.overflow = "scroll";
setTimeout(function(){
alert(obj.scrollHeight);
obj.style.overflow = "hidden";
}, 50);
}