Treat spaces as characters?

SeaBass

I'm running the following code to adjust the width of an input based on the content. It works pretty well, except if I add spaces to the end, then the text moves out of the box towards the left. How can I treat the spaces like any other characters so it doesn't break the box?

$.fn.liquidWidth = function(text, font) {

  if (!$.fn.liquidWidth.fakeEl) $.fn.liquidWidth.fakeEl = $('<span>').hide().appendTo(document.body);

  $.fn.liquidWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));

  var width = $.fn.liquidWidth.fakeEl.width();

  if (this.data('liquid-min-width') != '' && width < this.data('liquid-min-width')) {
    width = this.data('liquid-min-width');
  } else if (this.data('liquid-max-width') != '' && width > this.data('liquid-max-width')) {
    width = this.data('liquid-max-width');
  }
  if (width < 8) {
    width = 8;
  }

  return width;
};
$(document).on('input', '.liquid-width', function() {
  var inputWidth = $(this).liquidWidth();
  $(this).css({
    width: inputWidth
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Type something followed by a bunch of spaces...</p>
<input type="text" class="liquid-width" placeholder="text">

Ason

That won't work as is, because when you add space to a standard HTML element, it only render one, no matter how many you put in a row, which is quite different for e.g. an input, which renders them all.

For your span to also size like an input, use white-space: pre

or replace its "space"'s with &nbsp;

Stack snippet

$.fn.liquidWidth = function(text, font) {

	if (!$.fn.liquidWidth.fakeEl) $.fn.liquidWidth.fakeEl = $('<span>').hide().appendTo(document.body);

	$.fn.liquidWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));

	var width = $.fn.liquidWidth.fakeEl.width();

	if( this.data('liquid-min-width') != '' && width < this.data('liquid-min-width') )
	{
		width = this.data('liquid-min-width');
	}
	else if( this.data('liquid-max-width') != '' && width > this.data('liquid-max-width') )
	{
		width = this.data('liquid-max-width');
	}
	if( width < 8 ) { width = 8; }

	return width;
};
$(document).on('input', '.liquid-width', function() {
	var inputWidth = $(this).liquidWidth();
	$(this).css({
		width: inputWidth
	});
});
span {
  white-space: pre
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Type something followed by a bunch of spaces...</p>
<input type="text" class="liquid-width" placeholder="text">

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Treat spaces as spaces after n column

How to treat strings with spaces as a whole in bash?

add a single spaces in list containing characters

All Characters Around Keyword but Stop at Spaces

Remove white spaces before certain characters

Count characters in a string (excluding spaces) in R?

Read a text file and remove all characters except alphabets & spaces in Python

iOS how to deal with spaces or special characters passed to AFHTTPSessionManager GET method?

RegEx - 20 Characters long numbers only with spaces How

Find word inside special characters or spaces in multiple files

How to strip non-alphabetic, spaces and numeric characters from a string?

Using regEx to auto enable/disable button depending on characters and spaces

How to replace special characters and numbers with the character and spaces on each side

Remove spaces between characters in cell, but only if the space exists between two single characters

How to use cp command in bash script to copy files with spaces ans $ characters in it?

Python: Splitting words in text file into limited 40 characters AND filling the extra "slot" with spaces

Treat a string as a file in python

Haskell: treat newtype as String

Bash - treat parameters as command parameters

Sonarqube treat AOP Throwable as an issue

Force `unique` to treat NaNs as indistinct

Rlang: how to treat strings as symbols

How does iptables treat hostnames?

Treat N/A as zero within an IF formula

Repeating and spaces

Replace spaces with nonbreakable spaces in JSF

How does Mercurial treat equivalent changesets?

How to treat supposed chkrootkit false positive

Laravel treat www as default with wildcard subdomain

TOP 一覧

  1. 1

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  2. 2

    Modbus Python Schneider PM5300

  3. 3

    Ansibleで複数行のシェルスクリプトを実行する方法

  4. 4

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  5. 5

    どのように関係なく、それがどのように「悪い」、すべてのSSL証明書でのHttpClientを使用しないように

  6. 6

    インデックス作成時のドキュメントの順序は、Elasticsearchの検索パフォーマンスを向上させますか?

  7. 7

    ラベルとエントリがpythontkinterに表示されないのはなぜですか?

  8. 8

    Chromeウェブアプリのウェブビューの高さの問題

  9. 9

    パンダは異なる名前の列に追加します

  10. 10

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  11. 11

    Elasticsearch - あいまい検索は、提案を与えていません

  12. 12

    変数値を含むElasticSearch検索結果

  13. 13

    グラフ(.PNG)ファイルをエクスポートするZabbix

  14. 14

    Elasticsearchの場合、間隔を空けた単語を使用したワイルドカード検索

  15. 15

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  16. 16

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  17. 17

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  18. 18

    Elasticsearchでサーバー操作を最適化:低いディスク透かしに対処する

  19. 19

    テキストフィールドの値に基づいて UIslider を移動します

  20. 20

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  21. 21

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

ホットタグ

アーカイブ