0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ブラウザー上でも4桁のtabを入力したい

Posted at

TEXTAREAでTabキーを入力したい

ブラウザー上ではTabキーを打つとフォーカスが次の入力欄に移ります。当たり前ですよね。
なので仕方なくコピペでTabを入力したりします。それとTab幅は4桁にしてほしい(Qiitaの投稿欄も…)。
色分け表示のテキストエディターは各種ありますが、もっと軽いもので良いんです。

tabbedText.html
<textarea></textarea>
<style>
textarea {
	-moz-tab-size: 4;	/* for Firefox */
	-o-tab-size: 4;		/* for Opera */
	tab-size: 4;
	font-family:monospace;
}
</style>
<script src="tabbedText.js" type="text/javascript"></script>
tabbedText.js
textarea=document.getElementsByTagName('TEXTAREA');
for(i=0;i<textarea.length;i++){
	textarea[i].addEventListener('keydown', function(event){
		switch(event.key){
		case 'Tab':
			event.preventDefault();
			pos=event.target.selectionStart;
			val=event.target.value;
			val=val.substr(0,pos)+String.fromCharCode(event.keyCode)+val.substr(pos);
			event.target.value=val;
			event.target.selectionStart=pos+1;
			event.target.selectionEnd=pos+1;
			return false;
		default:
			return true;	// 通常の文字入力をさせる
		}
		return false;
	},false);
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?