2006년 5월 30일 화요일

TextArea에서 Tab이동 구현하기

 
      function doTab(arg1,arg2){
           textArea=arg2
           var keyCode;
           // key 값을 얻어옵니다.
           if ("which" in arg1){
               keyCode=arg1.which   
           }else if("keyCode" in arg1){
               keyCode=arg1.keyCode   
           }else if("keyCode" in window.event){
               keyCode=window.event.keyCode
           }else if("which" in window.event){
               keyCode=arg1.which
           }else{
               return true   
           }
          
           //tab을 삽입하고 시간이 지난 후에 다시 포커스를 줍니다.
           if (keyCode == 9) {
               textArea.value = textArea.value + "\t";
               setTimeout("textArea.focus()",1)
               return false;
           }
           return true
       }
Tab|Shift-Tab은 특수 키이므로 그냥 동작하지 않습니다. 따라서 setTimeout으로 시간이 지난후에 다시 포커스를 주는 편법으로 이를 해결합니다. 이게 해결책인지는 ...

출처 :
   http://www.webdeveloper.com/forum/archive/index.php/t-75879.html





댓글 2개:

  1. 뭐. 이미 늦어 버렸겠지만...

    다음과 같은 방법도 있다고 합니다.





    출처 : http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=40863



    <script>

    function blockKey(item){

    if(event.keyCode==9) {

    item.focus();

    space = " ";

    item.selection=document.selection.createRange();

    item.selection.text=space;

    event.returnValue = false;

    }

    }



    </script>

    <textarea id="textField" onkeydown="blockKey(this);" cols="70" rows="10"></textarea>





    공백 대신에 \t 를 써도 된다는군요 ^_^;



    물론 -_- 잘 동작하는지 확인은 안해 봤습니다. ㅎㅎ

    답글삭제