我正在使用此javascript代码来增加跨度中文本的字体大小,以使其填充其父div。

    function maximise_font(the_span, the_div, the_fontsize) {
        var fontSize = the_fontsize;
        var ourText = the_span;
        var maxHeight = the_div.height();
        var maxWidth = the_div.width();
        var textHeight;
        var textWidth;
        do {
            ourText.css('font-size', fontSize);
            textHeight = ourText.height();
            textWidth = ourText.width();
            fontSize = fontSize - 1;
        } while (textHeight > maxHeight || textWidth > maxWidth && fontSize > 3);
    }


它在Firefox中可以正常工作,但在Chrome中则不能。在Chrome中,内部循环的运行时间与Firefox中的运行时间相同,但是字体大小完全不变。这是为什么?

最佳答案

假设ourText是一个jQuery对象,看起来这行是您的问题:

ourText.css('font-size', fontSize);


您需要指定单位类型:

ourText.css('font-size', fontSize + "px");


我猜测Firefox只是为您做一些假设。

10-05 20:28
查看更多