用简单的Javascript函数来查找高度 [英] Stuck with simple Javascript function to find height

查看:214
本文介绍了用简单的Javascript函数来查找高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么下面的代码无法工作?

 < script type =text /的javascript> 
函数findheight(){
var div = document.getElementById('bg');
var height = document.scrollHeight;
div.style.height = height +'px';
}

< / script>
< / head>

< body onload =findheight()>
< div id =bg>

如果不是,还有其他的解决办法吗?任何帮助表示赞赏。谢谢

解决方案

你可以用javascript实现这个,你必须使用'offsetHeight'方法。


  function getHeight(){
var height = document.getElementById('test')。offsetHeight ;
alert(height);

下面是一个示例小提琴

您可以找到详情这里


Anybody knows why the following code fails to work? I need a dynamic height for a fixed width div that may descend below past the viewport.

 <script type="text/javascript">
function findheight(){  
  var div = document.getElementById('bg');
  var height = document.scrollHeight;
  div.style.height = height + 'px';
}

  </script>     
</head>

<body onload="findheight()">
    <div id="bg"> 

Is there another way around this if not?? Any help is appreciated. Thanks

解决方案

You could achieve this using javascript, you have to use the 'offsetHeight' method.

function getHeight() {
    var height = document.getElementById('test').offsetHeight;
    alert(height);
}

Here is a sample fiddle
You can find the details here

这篇关于用简单的Javascript函数来查找高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆