【问题标题】:Find current cursor position for textbox with htmleditorextender使用 htmleditorextender 查找文本框的当前光标位置
【发布时间】:2013-10-01 14:11:18
【问题描述】:

我一直在使用 insertAtCaret 在文本框的当前光标位置插入文本,但是当文本框与 htmleditorextender 关联时它会失败(我猜是因为文本框现在是 htmleditorextender 的子元素,或者它创建了一个 iframe? )。

这是我的 insertAtCaret javascript,在没有 htmleditorextender 时可以正常工作。与 htmleditorextender 关联的文本框是 txtEmailBody。我需要以不同的方式引用 txtEmailBody 吗?

function insertAtCaret(text) {
     var txtarea = document.getElementById("<%=txtEmailBody.ClientID%>")
     var scrollPos = txtarea.scrollTop;
     var strPos = 0;
     var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
    "ff" : (document.selection ? "ie" : false));
     if (br == "ie") {
         txtarea.focus();
         var range = document.selection.createRange();
         range.moveStart('character', -txtarea.value.length);
         strPos = range.text.length;
     }
     else if (br == "ff") strPos = txtarea.selectionStart;

     var front = (txtarea.value).substring(0, strPos);
     var back = (txtarea.value).substring(strPos, txtarea.value.length);
     txtarea.value = front + text + back;
     strPos = strPos + text.length;
     if (br == "ie") {
         txtarea.focus();
         var range = document.selection.createRange();
         range.moveStart('character', -txtarea.value.length);
         range.moveStart('character', strPos);
         range.moveEnd('character', 0);
         range.select();
     }
     else if (br == "ff") {
         txtarea.selectionStart = strPos;
         txtarea.selectionEnd = strPos;
         txtarea.focus();
     }
     txtarea.scrollTop = scrollPos;

 } 

谢谢。

【问题讨论】:

  • 这将在特定位置插入文本,但我仍然需要找到当前光标位置,以便可以在该点插入 txt:

标签: htmleditorextender


【解决方案1】:

答案在这里http://jsfiddle.net/VzbYJ/32/

只需将 htmleditorextender 的引用更改如下:

$(".btn").click(function () {
        var el = $find("<%= HtmlEditorExtender3.ClientID %>")._editableDiv;
        pasteHtmlAtCaret("Some <b>random</b> text", el);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2011-10-21
    相关资源
    最近更新 更多