标签: html

  • 用Jquery让textarea支持自动缩进

    时效性提醒:本文首次编写发布于11 年前。

    这个代码的作用是让textarea也能输入tab,并可以像代码编辑器一样能自动缩进。

    废话不多说了,直接上Demo:

    https://codepen.io/Wu23333/pen/MWwYmmK

    var textarea_id = "text";
     (function($, undefined) {
             $.fn.getCursorPosition = function() {
             var el = $(this).get(0);
             var pos = 0;
             if ('selectionStart' in el) {
                 pos = el.selectionStart;
             }else
                 if ('selection' in document) {
                     el.focus();
                     var Sel = document.selection.createRange();
                     var SelLength = document.selection.createRange().text.length;
                     Sel.moveStart('character', -el.value.length);
                     pos = Sel.text.length - SelLength;
                 }
             return pos;
         }
     })(jQuery);
     $(document).keydown(function(e){
         text=document.getElementById(textarea_id);
         if(e.which == 9){
             var cursor_pos = $(text).getCursorPosition();
             $(text).val($(text).val().slice(0,cursor_pos) + '\t' + $(text).val().slice(cursor_pos));
             text.focus();
             text.setSelectionRange(cursor_pos+1, cursor_pos+1);
             return false;
         }
         if(e.which == 13){
             var cursor_pos = $(text).getCursorPosition();
             var notelines = $(text).val().slice(0,cursor_pos).split('\n');
             var listline = notelines[notelines.length-1];
             var n = 0;
             while(listline[n] == '\t'){
                 n+=1;
             }
             $(text).val($(text).val().slice(0, cursor_pos)+'\n'+$(text).val().slice(cursor_pos));
             for (i=n; i>0; i--){
                 $(text).val($(text).val().slice(0, cursor_pos+1)+'\t'+$(text).val().slice(cursor_pos+1));
             }
             text.focus();
             text.setSelectionRange(cursor_pos + n+1, cursor_pos + n+1);
             return false;
         }
     });
  • HTML在线测试工具,欢迎使用

    时效性提醒:本文首次编写发布于12 年前。

    刚写的,功能还算不错.

    地址:wusiyu.tk/html

    PS:此工具已开源,下载地址:GIT@OSC

    PPS:这个站的主题是我写的,也已开源,地址:GIT@OSC ,注意test分支才是最新的,和这个站的一样,回头把注释什么的补全再并合到主分支.