【问题标题】:Insert text at current cursor position on dropdown list changed inside iframe在 iframe 内更改的下拉列表中的当前光标位置插入文本
【发布时间】:2014-03-29 12:45:55
【问题描述】:

我正在使用 Microsoft ajax-toolkit 提供的文本编辑器。
它在浏览器上呈现iframe。我在该编辑器中添加了一个下拉列表,我希望当用户更改下拉索引时,应将值添加到编辑器当前光标位置。

我在 SO 上获得了一个代码,它为我提供了编辑器中当前选定的文本如下

function getIframeSelectionText(iframe) {
        var win = iframe.contentWindow;
        var doc = iframe.contentDocument || win.document;

        if (win.getSelection) {
            return win.getSelection().toString();

        } else if (doc.selection && doc.selection.createRange) {
            return doc.selection.createRange().text;
        }
 }

但我想在当前位置添加一些文本。 html呈现如下

<td class="ajax__htmleditor_editor_editpanel"><div id="Editor1_ctl02" style="height:100%;width:100%;">
            <iframe id="Editor1_ctl02_ctl00" name="Editor1_ctl02_ctl00" marginheight="0" marginwidth="0" frameborder="0" style="height:100%;width:100%;display:none;border-width:0px;">

            </iframe><textarea id="Editor1_ctl02_ctl01" class="ajax__htmleditor_htmlpanel_default" style="height:100%;width:100%;display:none;"></textarea><iframe id="Editor1_ctl02_ctl02" name="Editor1_ctl02_ctl02" marginheight="0" marginwidth="0" frameborder="0" style="height:100%;width:100%;display:none;border-width:0px;">

            </iframe>
        </div></td>

我正在尝试如下

$("#imgDropdown").change(function () {
            //var iframeBody =    $(window.Editor1_ctl02_ctl00.document.getElementsByTagName("body")[0]);
            var iframe = document.getElementById("Editor1_ctl02_ctl00");
            $("#Editor1_ctl02_ctl00").find("body").insertAtCaret("value");
            //alert(getIframeSelectionText(iframe));
        });

插入文本的功能不适用于iframe如下

$.fn.extend({
        insertAtCaret: function (myValue) {
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else if (this.selectionStart || this.selectionStart == '0') {
                var startPos = this.selectionStart;
                var endPos = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
                this.focus();
                this.selectionStart = startPos + myValue.length;
                this.selectionEnd = startPos + myValue.length;
                this.scrollTop = scrollTop;
            } else {
                this.value += myValue;
                this.focus();
            }
        }
    })

【问题讨论】:

  • 任何控制台错误(例如,安全问题)?
  • 具体是什么不起作用?您能否为我们测试是否使用正确的参数等正确调用了该函数?
  • $("#Editor1_ctl02_ctl00").find("body").insertAtCaret("value"); 不起作用,它应该将值插入当前指针所在的正文中。
  • 你能提供一个jsfiddle吗?
  • 代码是否正确进入 .insertAtCaret() 函数?你能成功地在那里放置一个警报来测试它是否进入了那个函数?

标签: javascript jquery ajax html-editor


【解决方案1】:

简单,你只需要使用。

$("#Editor1_ctl02_ctl00").contents().find('textarea').insertAtCaret('value');

更新

抱歉,我认为 insertAtCaret 函数对你有用,你只需要在 iFrame 中工作。您可以使用此版本的 insertAtCaret

jQuery.fn.extend({
    insertAtCaret: function (html) {
        var winObject = function (el){
            var doc = el.ownerDocument;
            return doc.defaultView || doc.parentWindow
        };
        return this.each(function (i) {
                var sel, range, w = this;
                w = winObject(w);
                if (w.getSelection) {
                    // IE9 and non-IE
                    sel = w.getSelection();
                    if (sel.getRangeAt && sel.rangeCount) {
                        range = sel.getRangeAt(0);
                        range.deleteContents();

                        // Range.createContextualFragment() would be useful here but is
                        // only relatively recently standardized and is not supported in
                        // some browsers (IE9, for one)
                        var el = w.document.createElement("div");
                        el.innerHTML = html;
                        var frag = w.document.createDocumentFragment(), node, lastNode;
                        while ((node = el.firstChild)) {
                            lastNode = frag.appendChild(node);
                        }
                        range.insertNode(frag);

                        // Preserve the selection
                        if (lastNode) {
                            range = range.cloneRange();
                            range.setStartAfter(lastNode);
                            range.collapse(true);
                            sel.removeAllRanges();
                            sel.addRange(range);
                        }
                    }
                } else if (w.document.selection && w.document.selection.type != "Control") {
                    // IE < 9
                    w.document.selection.createRange().pasteHTML(html);
                }
            }
        )
    }
});

然后这样称呼它:

$("#Editor1_ctl02_ctl00").contents().find('body').insertAtCaret($val);

函数改编自here

编码愉快!

【讨论】:

  • 感谢您的回复,但我的编辑器中没有textarea
  • 感谢它在 Mozilla 中工作,但在 ie 中不工作。
  • 哪个版本的ie?在我的 IE 10 上工作。
【解决方案2】:

这里似乎有一些问题。

  1. Microsoft ajax-toolkit 编辑器创建了一个 iframe,其中 designMode 属性已打开,这就是它可编辑的原因,它没有任何价值,并且 textNodes 直接添加到正文中,这使它有点更难。

  2. 当您从下拉列表中选择某些内容时,焦点位于下拉列表中,并且没有插入符号位置,因为焦点已从 iFrame 移开。
    我假设下拉菜单位于编辑器的顶部菜单栏中或 iFrame 之外的任何其他位置。

此外,Microsoft ajax-toolkit 编辑器有一个推荐更新,HTMLEditorExtender

您必须捕获插入符号位置的代码似乎是用于常规输入/文本区域,并且您必须调整该代码以与处于设计模式的 iframe 内的任何节点一起使用,并具有自己的窗口和文档等等

鉴于以上考虑,这就是我想出的办法

var frameID  = 'Editor1_ctl02_ctl00',
    selectID = 'imgDropdown',
    iframe   = document.getElementById(frameID),
    iWin     = iframe.contentWindow ? iframe.contentWindow : window.frames[frameID];

$(iWin).on('blur', function() {
    $(iframe).data('range', getRange(iWin));
});

$('#' + selectID).on('change', function() {
    var range = $(iframe).data('range');
    addText(iWin, range, this.value);
});


function getRange(win) {
    var sel, range, html;
    if (win.getSelection) {
        sel = win.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();
        }
    } else if (win.document.selection && win.document.selection.createRange) {
        range = win.document.selection.createRange();
    }
    return range;
}

function addText(win, range, text) {
    if (win.getSelection) {
       range.insertNode(win.document.createTextNode(text));
    } else if (win.document.selection && win.document.selection.createRange) {
        range.text = text;
    }
}

FIDDLE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多