Calculating text width

后端 未结 22 1325
轻奢々
轻奢々 2020-11-22 03:36

I\'m trying to calculate text width using jQuery. I\'m not sure what, but I am definitely doing something wrong.

So, here is the code:

var c = $(\'.c         


        
相关标签:
22条回答
  • 2020-11-22 03:56

    Slight change to Nico's, since .children() will return an empty set if we're referencing a text element like an h1 or p. So we'll use .contents() instead, and use this instead of $(this) since we're creating a method on a jQuery object.

    $.fn.textWidth = function(){
        var contents = this.contents(),
            wrapper  = '<span style="display: inline-block;" />',
            width    = '';
    
        contents.wrapAll(wrapper);
        width = contents.parent().width(); // parent is now the wrapper
        contents.unwrap();
        return width;
        };
    
    0 讨论(0)
  • 2020-11-22 03:57
    $.fn.textWidth = function(){
            var w = $('body').append($('<span stlye="display:none;" id="textWidth"/>')).find('#textWidth').html($(this).html()[0]).width();
            $('#textWidth').remove();
            console.log(w);
            return w;
        };
    

    almost a one liner. Gives you the with of the first character

    0 讨论(0)
  • 2020-11-22 03:59

    jQuery's width functions can be a bit shady when trying to determine the text width due to inconsistent box models. The sure way would be to inject div inside your element to determine the actual text width:

    $.fn.textWidth = function(){
      var sensor = $('<div />').css({margin: 0, padding: 0});
      $(this).append(sensor);
      var width = sensor.width();
      sensor.remove();
      return width;
    };
    

    To use this mini plugin, simply:

    $('.calltoaction').textWidth();
    
    0 讨论(0)
  • 2020-11-22 03:59

    text width can be different for different parents, for example if u add a text into h1 tag it will be wider than div or label, so my solution like this:

    <h1 id="header1">
    
    </h1>
    
    alert(calcTextWidth("bir iki", $("#header1")));
    
    function calcTextWidth(text, parentElem){
        var Elem = $("<label></label>").css("display", "none").text(text);
        parentElem.append(Elem);
      var width = Elem.width();
      Elem.remove();
        return width;
    }
    
    0 讨论(0)
  • 2020-11-22 04:01

    I found this solution works well and it inherits the origin font before sizing:

    $.fn.textWidth = function(text){
      var org = $(this)
      var html = $('<span style="postion:absolute;width:auto;left:-9999px">' + (text || org.html()) + '</span>');
      if (!text) {
        html.css("font-family", org.css("font-family"));
        html.css("font-size", org.css("font-size"));
      }
      $('body').append(html);
      var width = html.width();
      html.remove();
      return width;
    }
    
    0 讨论(0)
  • 2020-11-22 04:02

    Call getColumnWidth() to get the with of the text. This works perfectly fine.

    someFile.css
    .columnClass { 
        font-family: Verdana;
        font-size: 11px;
        font-weight: normal;
    }
    
    
    function getColumnWidth(columnClass,text) { 
        tempSpan = $('<span id="tempColumnWidth" class="'+columnClass+'" style="display:none">' + text + '</span>')
              .appendTo($('body'));
        columnWidth = tempSpan.width();
        tempSpan.remove();
    return columnWidth;
    }
    

    Note:- If you want inline .css pass the font-details in style only.

    0 讨论(0)
提交回复
热议问题