最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to know Placeholder String width in px by JqueryJavascript? - Stack Overflow

programmeradmin4浏览0评论

Here is the 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>

I want to know the text width only. Not the input width nor the number of character. Please help me with a proper suggestion.

Here is the 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>

I want to know the text width only. Not the input width nor the number of character. Please help me with a proper suggestion.

Share Improve this question edited Jun 6, 2014 at 11:37 Subhajit Panja asked Jun 6, 2014 at 11:24 Subhajit PanjaSubhajit Panja 1,2401 gold badge9 silver badges22 bronze badges 6
  • for width text, you mean at the length or pixels? – napster3world Commented Jun 6, 2014 at 11:27
  • he want the width taken by placeholder text. – Amit Kumar Commented Jun 6, 2014 at 11:30
  • ok, but number of chars ex.(17 characters) or width ex. (100px)? – napster3world Commented Jun 6, 2014 at 11:31
  • @napster3world How to know Placeholder String width in px – A. Wolff Commented Jun 6, 2014 at 11:33
  • 2 +1 for a great question – codefreaK Commented Jun 6, 2014 at 11:40
 |  Show 1 more ment

1 Answer 1

Reset to default 10

You could use following snippet:

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

--DEMO--

发布评论

评论列表(0)

  1. 暂无评论