开发者

safari returning value "normal" for "line-height" style

Here is my problem : when i call document.defaultView.getComputedStyle(Node, "").getPropertyValue("line-height")

safari returns the string "normal" when no line-height is specified, whereas firefox always returns the value in pixels.

This poses 开发者_如何学Pythonme a big problem, especilly because I didn't found a reliable formula to get the numeric value (I found it should be between 1em and 1.3 but nothing precise).

Is there a solution?


Well I found a partial solution :

function getLineHeight(node){
        var clh=document.defaultView.getComputedStyle(node, "").getPropertyValue("line-height");
        var copy;
        if (clh=="normal"){

            copy=node.cloneNode(false);
            copy.style.padding=0+"px";
            copy.style.border="none";
            copy.innerHTML="x <br> x <br> x <br> x <br> x <br> x <br> x <br> x <br> x <br> x";
            node.parentNode.insertBefore(copy, node);
            clh=copy.offsetHeight/10;
            node.parentNode.removeChild(copy);
            delete copy;
        }
        else clh=parseFloat(clh);
        return clh;
    } 

For those that are wondering why all those "br" in the "innerHTML" well offsetHeight returns integer values, so using a 10 line node and dividing the result by 10 gives more accurate answer.


No, there is no solution. The defaults will always differ between browsers, as there is no standard for what the defaults should be.

Users can set their own style sheet to override the standards, so you can even get different result from two different instances of the same browser.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜