javascript - 动态调整div的高度

标签 javascript css xhtml

嗨,我正在找这个:Dynamically adjust an iframe's height但是为了让它对一个 div 执行此操作,到目前为止我有以下内容:

#NewsBlock {
height: 222px;
overflow-x: auto;
padding: 5px;
}



<div class="boxFive">
<h3 class="tab">Latest News</h3>
<script type="text/javascript">
function resizeIframe() {
    var height = document.documentElement.clientHeight;
height -= document.getElementById('NewsBlock').offsetTop;

height -= 20; /* whatever you set your body bottom margin/padding to be */
document.getElementById('NewsBlock').style.height = height +"px";

};
document.getElementById('NewsBlock').onload = resizeIframe;
window.onresize = resizeIframe;
</script>
                    <div id="NewsBlock">
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>
                        <div class="ShortNews">
                            <a href="" class="NewsLink"></a>
                            <p>title for news article<a href="#" title="Click to read more">more...</a></p>
                        </div>

                    </div>
                </div>

但它似乎没有用,我是不是漏掉了什么?

最佳答案

尝试:

function resizeIframe() {
  var height = 0;
  var body = window.document.body;
  if (window.innerHeight) {
      height = window.innerHeight;
  } else if (body.parentElement.clientHeight) {
      height = body.parentElement.clientHeight;
  } else if (body && body.clientHeight) {
      height = body.clientHeight;
  }
  document.getElementById('NewsBlock').style.height = ((height - document.getElementById('NewsBlock').offsetTop) + "px");
}

关于javascript - 动态调整div的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404430/

相关文章:

javascript - 我怎样才能制作像 http ://www. 20thingsilearned.com/这样的在线书籍

javascript - 在不禁用 Javascript 的情况下在 WebView 中禁用 HTML5

html - 我在我的 cpanel 中上传了图片,但出现 404 错误,而且我在我的开发工具中找不到它们?

javascript - 如何使用 javascript 或 jquery 迭代自己制作的 xhtml 节点

java - JDBC mysql 从数据库设置和检索数据

javascript - document.body.innerHTML 问题

javascript - 将元素隐藏在具有类的元素内

javascript - 如何调整 Caroufredsel slider 的上边距?

html - 用于 mozilla 和 safari 的样式表栏

html - 表格宽度比指定的像素长?