开发者

Document Height in Javascript

help to make an equivalent Javascript code for the Jquery code below

  <script>
var httmp=parseInt($(document).height());
var htorg 开发者_Go百科  = parseInt(httmp-71)
$("div.content_box").height( htorg);
    </script>


Here is my attempt to get javascript code for that:

// 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';

If you have multiple elements with same class, you should modify the loop like this:

// 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';
  }
}

That should now set the height of all elements that have class content_box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜