簡體   English   中英

為什么增加字體大小的此javascript代碼在Firefox中有效,而在Chrome中卻無效?

[英]Why does this javascript code to increase font size work in Firefox but not in Chrome?

我正在使用此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只是在為您做一些假設。

您可以顯示您的html外觀嗎? 我在這里進行了測試,它對於Firefox和chrome都可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM