4

this.$el.find('#divname').append(newDivHtml)将附加到newDivHTML现有的div. 但是,我正在研究这种情况:

  1. 在现有div1用户上编辑一些自由文本并单击按钮
  2. 单击按钮时,弹出视图会显示选项列表。
  3. 选择一个选项会将 插入newDivHtml到 underlaying 的光标位置div1

我试过了:http : //jsfiddle.net/jwvha/1/

 function pasteHtmlAtCaret(html) {
        var sel, range;
        if (window.getSelection) {
        // IE9 and non-IE
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();

            // Range.createContextualFragment() would be useful here but is
            // non-standard and not supported in all browsers (IE9, for one)
            var el = document.createElement("div");
            el.innerHTML = html;
            var frag = 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 (document.selection && document.selection.type != "Control") {
        // IE < 9
        document.selection.createRange().pasteHTML(html);
    }
}

2. doesn't seems to work for that.$e1.find('#sectionContentSimple').insertAdjacentHTML(100, newhtml);
4

1 回答 1

0

只是一个没有深入研究代码的想法,您是否尝试过将单击的元素保存到变量中以便以后可以访问它,而不是依靠它来保持焦点?

于 2013-07-12T14:00:02.200 回答