Javascript中的文档高度

时间:2011-02-19 10:24:17

标签: javascript jquery xhtml

帮助为下面的Jquery代码创建等效的Javascript代码

  <script>
var httmp=parseInt($(document).height());
var htorg   = parseInt(httmp-71)
$("div.content_box").height( htorg);
    </script>

1 个答案:

答案 0 :(得分:2)

这是我尝试获取javascript代码:

// Cross browser function to get document height
// http://james.padolsey.com/javascript/get-document-height-cross-browser/
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

var httmp = getDocHeight();
var htorg = parseInt(httmp - 71);

var elms = document.getElementsByTagName('div');
var theDiv = null;

// search for element with class content_box
for (var i = 0; i < elms.length; i++){
  if (elms[i].getAttribute('class') === 'content_box'){
    theDiv = elms[i];
    break;
  }
}

theDiv.style.height = htorg + 'px';

如果你有多个具有相同类的元素,你应该像这样修改循环:

// search for element with class content_box
for (var i = 0; i < elms.length; i++){
  if (elms[i].getAttribute('class') === 'content_box'){
    theDiv = elms[i];
    theDiv.style.height = htorg + 'px';
  }
}

现在应该设置所有具有类height的元素的content_box