Editing Iframe Content in IE - problem in maintaining text selection

后端 未结 3 1055
春和景丽
春和景丽 2020-12-12 02:01

can somebody please guide me the proper way if this is possible? I was actually trying to make a text editor using iframe with designMode=\'on\'. The buttons in the editing

3条回答
  •  佛祖请我去吃肉
    2020-12-12 02:33

    If you're not changing the editor frame's DOM between it losing and regaining focus then the following functions will do: call saveSelection(iframeWindow) before the editor document loses focus and restoreSelection(iframeWindow, sel) after it regains focus. Otherwise, I'd suggest using my Rangy library's save/restore selection module, which uses hidden marker elements.

    var saveSelection, restoreSelection;
    if (window.getSelection) {
        // IE 9 and non-IE
        saveSelection = function(win) {
            var sel = win.getSelection(), ranges = [];
            if (sel.rangeCount) {
                for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                    ranges.push(sel.getRangeAt(i));
                }
            }
            return ranges;
        };
    
        restoreSelection = function(win, savedSelection) {
            var sel = win.getSelection();
            sel.removeAllRanges();
            for (var i = 0, len = savedSelection.length; i < len; ++i) {
                sel.addRange(savedSelection[i]);
            }
        };
    } else if (document.selection && document.selection.createRange) {
        // IE <= 8
        saveSelection = function(win) {
            var sel = win.document.selection;
            return (sel.type != "None") ? sel.createRange() : null;
        };
    
        restoreSelection = function(win, savedSelection) {
            if (savedSelection) {
                savedSelection.select();
            }
        };
    }
    

提交回复
热议问题