• <bdo id='EZF5g'></bdo><ul id='EZF5g'></ul>

    <legend id='EZF5g'><style id='EZF5g'><dir id='EZF5g'><q id='EZF5g'></q></dir></style></legend>

    1. <tfoot id='EZF5g'></tfoot>
      <i id='EZF5g'><tr id='EZF5g'><dt id='EZF5g'><q id='EZF5g'><span id='EZF5g'><b id='EZF5g'><form id='EZF5g'><ins id='EZF5g'></ins><ul id='EZF5g'></ul><sub id='EZF5g'></sub></form><legend id='EZF5g'></legend><bdo id='EZF5g'><pre id='EZF5g'><center id='EZF5g'></center></pre></bdo></b><th id='EZF5g'></th></span></q></dt></tr></i><div id='EZF5g'><tfoot id='EZF5g'></tfoot><dl id='EZF5g'><fieldset id='EZF5g'></fieldset></dl></div>

        <small id='EZF5g'></small><noframes id='EZF5g'>

      1. 用 JavaScript 计算文本宽度

        时间:2023-08-07
        • <bdo id='Bjcqi'></bdo><ul id='Bjcqi'></ul>
        • <i id='Bjcqi'><tr id='Bjcqi'><dt id='Bjcqi'><q id='Bjcqi'><span id='Bjcqi'><b id='Bjcqi'><form id='Bjcqi'><ins id='Bjcqi'></ins><ul id='Bjcqi'></ul><sub id='Bjcqi'></sub></form><legend id='Bjcqi'></legend><bdo id='Bjcqi'><pre id='Bjcqi'><center id='Bjcqi'></center></pre></bdo></b><th id='Bjcqi'></th></span></q></dt></tr></i><div id='Bjcqi'><tfoot id='Bjcqi'></tfoot><dl id='Bjcqi'><fieldset id='Bjcqi'></fieldset></dl></div>
        • <small id='Bjcqi'></small><noframes id='Bjcqi'>

        • <tfoot id='Bjcqi'></tfoot>

                <tbody id='Bjcqi'></tbody>

              • <legend id='Bjcqi'><style id='Bjcqi'><dir id='Bjcqi'><q id='Bjcqi'></q></dir></style></legend>
                  本文介绍了用 JavaScript 计算文本宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  I'd like to use JavaScript to calculate the width of a string. Is this possible without having to use a monospace typeface?

                  If it's not built-in, my only idea is to create a table of widths for each character, but this is pretty unreasonable especially supporting Unicode and different type sizes (and all browsers for that matter).

                  解决方案

                  Create a DIV styled with the following styles. In your JavaScript, set the font size and attributes that you are trying to measure, put your string in the DIV, then read the current width and height of the DIV. It will stretch to fit the contents and the size will be within a few pixels of the string rendered size.

                  var fontSize = 12;
                  var test = document.getElementById("Test");
                  test.style.fontSize = fontSize;
                  var height = (test.clientHeight + 1) + "px";
                  var width = (test.clientWidth + 1) + "px"
                  
                  console.log(height, width);

                  #Test
                  {
                      position: absolute;
                      visibility: hidden;
                      height: auto;
                      width: auto;
                      white-space: nowrap; /* Thanks to Herb Caudill comment */
                  }

                  <div id="Test">
                      abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
                  </div>

                  这篇关于用 JavaScript 计算文本宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Moment.js amDateFormat 总是返回 1970 年的日期 下一篇:disabled 属性的正确值是多少?

                  相关文章

                  最新文章

                      <bdo id='UtlFK'></bdo><ul id='UtlFK'></ul>

                  1. <tfoot id='UtlFK'></tfoot>

                    <small id='UtlFK'></small><noframes id='UtlFK'>

                    <i id='UtlFK'><tr id='UtlFK'><dt id='UtlFK'><q id='UtlFK'><span id='UtlFK'><b id='UtlFK'><form id='UtlFK'><ins id='UtlFK'></ins><ul id='UtlFK'></ul><sub id='UtlFK'></sub></form><legend id='UtlFK'></legend><bdo id='UtlFK'><pre id='UtlFK'><center id='UtlFK'></center></pre></bdo></b><th id='UtlFK'></th></span></q></dt></tr></i><div id='UtlFK'><tfoot id='UtlFK'></tfoot><dl id='UtlFK'><fieldset id='UtlFK'></fieldset></dl></div>
                  2. <legend id='UtlFK'><style id='UtlFK'><dir id='UtlFK'><q id='UtlFK'></q></dir></style></legend>