开发者

Getting computed height of a hidden div

开发者 https://www.devze.com 2023-03-12 13:08 出处:网络
I am trying to calculate the computed height of a div with display:none with this function: function getComputedHeight(theElt){

I am trying to calculate the computed height of a div with display:none with this function:

function getComputedHeight(theElt){
    if(navigator.appName=='Microsoft Internet Explorer'){
        tmphght = document.getElementById(theElt).offsetHeight;
    }
    else{
        docObj = document.getEleme开发者_如何学JAVAntById(theElt);
        var tmphght1 = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("height");
        tmphght = tmphght1.split('px');
        tmphght = tmphght[0];
    }
    return tmphght;
}

This is my html

<a href="javascript:;" onclick="showme('<?php echo 'mydiv765_'.$userid[$i];?>')">View</a>

and the function called

function showme(objid)
{
    var h=getComputedHeight(objid);
    alert(h);   
}

The function returns: auto.

Please how can i fix this? Is there a better way of achieving same effect?


Try this?

function getComputedHeight(obj){

    var originalDisplay = obj.style.display; 

    if (originalDisplay == "none") {
       obj.style.filter = "alpha(opacity=1)";
       obj.style.opacity = 0.01;
    }

    height = obj.offsetHeight;

    if (originalDisplay == "none") {
        obj.style.display = originalDisplay; 
        obj.style.filter = "alpha(opacity=100)"; // IE
        obj.style.opacity = 1; // Firefox, etc..
    }
    return height;
}

Modified from Height of Hidden Div?

0

精彩评论

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