Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

          NOTE: I’ve released a set of JavaScript Quicktags (that implement this technique) under the LGPL license. Please look at this code as it has a variety of enhancements and bug fixes.

          I discovered a real JavaScript gem in PHPMyAdmin this morning. They have code that inserts content into a <textarea> at the cursor position instead of just at the beginning/end of the content. This is something I thought wasn’t possible – I’m quite pleased I was wrong.

          I’ll be glad to see this code spread quickly to web forms that use buttons to aid in styling text and inserting image and link tags. WordPress and other blog and web forum packages would really benefit from this. (PHPMyAdmin is GPL, so any other GPL app should be able to use their code directly with attribution).

          Here is a quick look at a generic version of the technique:

          function insertAtCursor(myField, myValue) {
          //IE support
          if (document.selection) {
          myField.focus();
          sel = document.selection.createRange();
          sel.text = myValue;
          }
          //MOZILLA/NETSCAPE support
          else if (myField.selectionStart || myField.selectionStart == '0') {
          var startPos = myField.selectionStart;
          var endPos = myField.selectionEnd;
          myField.value = myField.value.substring(0, startPos)
          + myValue
          + myField.value.substring(endPos, myField.value.length);
          } else {
          myField.value += myValue;
          }
          }
          // calling the function
          insertAtCursor(document.formName.fieldName, 'this value');
          I guess when the mouse leaves the textarea, it still has a cursor position left over. This code uses that position just as you’d want it to.
          posted on 2010-06-04 03:12 seal 閱讀(241) 評論(0)  編輯  收藏 所屬分類: Javascript
          主站蜘蛛池模板: 陆川县| 桓台县| 郑州市| 措勤县| 潞城市| 元阳县| 西林县| 忻城县| 和顺县| 修水县| 察隅县| 金昌市| 婺源县| 佛冈县| 阳高县| 天长市| 纳雍县| 吴忠市| 潼南县| 灵山县| 平武县| 大丰市| 沂南县| 兴化市| 湘潭市| 巴林左旗| 平遥县| 乌苏市| 远安县| 汤阴县| 梁山县| 蒙城县| 永和县| 连山| 蓬溪县| 印江| 舒兰市| 衡南县| 贵德县| 赤城县| 新巴尔虎右旗|