检测高度不起作用

时间:2011-07-08 01:02:52

标签: javascript

我有这个简单的脚本:

var o = document.getElementById("content");

if (o.clientHeight == "372") {
  o.style.height = "328px";
  o.style.marginBottom = "44px";

} else {
  o.style.height = "214px";
  o.style.marginBottom = "32px";
}

但不知何故ELSE总是执行,即使我的div的初始高度是372px ...... 有谁可以帮助我?

2 个答案:

答案 0 :(得分:2)

clientHeight考虑了填充。您可能需要使用scrollHeightoffsetHeightstyle.height,具体取决于您的需求。请注意,style.height不会返回与其他整数相似的整数。

http://help.dottoro.com/ljcadejj.php

答案 1 :(得分:-1)

如果clientHeight确实返回整数,请尝试删除这样的引号:

var o = document.getElementById("content");
    if(o.clientHeight==372){
    o.style.height="328px";
    o.style.marginBottom="44px";}
    else{o.style.height="214px";
    o.style.marginBottom="32px";}