【问题标题】:How to know Placeholder String width in px by Jquery/Javascript?如何通过 Jquery/Javascript 知道以 px 为单位的占位符字符串宽度?
【发布时间】:2014-07-27 15:44:56
【问题描述】:

这里是html

<body style="background-color:#999;font-size:12px;">
    <div style="width:500px; height:500px; margin:30px auto; background-color:#9CC">
    <input id="Test" type='text' placeholder='Hello how are you' style="width:270px;" / >
    </div>
</body>

我只想知道文本宽度。 不是输入宽度也不是字符数。请帮我提出适当的建议。

【问题讨论】:

  • 对于宽度文本,您是指长度还是像素?
  • 他想要占位符文本的宽度。
  • 好的,但是字符数例如(17 个字符)或宽度例如。 (100 像素)?
  • @napster3world How to know Placeholder String width in px
  • +1 提出一个好问题

标签: javascript jquery string placeholder


【解决方案1】:

您可以使用以下 sn-p:

var _$tmpSpan = $('<span/>').html($('#Test').attr('placeholder')).css({
    position: 'absolute',
    left: -9999,
    top: -9999
}).appendTo('body'),
    textWidth = _$tmpSpan.width();
_$tmpSpan.remove();
alert(textWidth);

--DEMO--

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2011-06-28
  • 2011-09-02
  • 2011-09-15
相关资源
最近更新 更多