javascript - 在插入符号位置插入嵌套元素

标签 javascript jquery html dom rangy

我正在将 contenteditable div 与 range 结合使用,在 HTML 中的插入符位置插入、删除、更改、注释和格式化选项。 我已经使用范围函数 SplitAtcaret 实现了插入新段落:http://jsfiddle.net/timdown/rr9qs/2/ 。这运作良好。

现在我需要实现相同的插入新部分,就像部分中的段落概念一样。

部分例如:

输入 HTML:

<div class="section_level1" >
    <h1>First Level section Heading</h1>
    <p>Level First some text some text some paragraphs. </p>
    <div class="section_level2" >
        <h1>Second Level section Heading</h1>
        <p>Second level some text <rangy position> insert new section (level 1)  here  </rangy posiiton > some text some paragraphs. </p>
        <div class="section_level3" >
            <h1>Third Level section Heading</h1>
            <p>Third level some text dummy text dummy text.some paragraphs. </p>
        </div>
    </div>
</div>

需要输出 Html 为(在插入符位置(第二级内)插入新部分时。):

<div class="section_level1" >
    <h1>First Level section Heading</h1>
    <p>Level First some text some text some paragraphs. </p>
    <div class="section_level2" >
        <h1>Second Level section Heading</h1>
        <p>Second level some text <p>
    </div>
</div>
<div class="section_level1" >  
    <h1>Level 1 heading goes here </h1>
    <p> Level 1 contents goes here. </p>
    <div class="section_level2" >
        <h1>dummy heading</h1>
        <p>Second level some text <p>
        <div class="section_level3" >
            <h1>Third Level section Heading</h1>
            <p>Third level some text dummy text dummy text.some paragraphs. </p>
        </div>
    </div>
</div>

最佳答案

您可以使用 Tim Down 的 splitParaAtCaret :

function splitParaAtCaret() {
    var sel = rangy.getSelection();
    if (sel.rangeCount > 0) {
        // Create a copy of the selection range to work with
        var range = sel.getRangeAt(0).cloneRange();

        // Get the containing paragraph
        var p = range.commonAncestorContainer;
        while (p && (p.nodeType != 1 || p.tagName != "P") ) {
            p = p.parentNode;
        }

        if (p) {
            // Place the end of the range after the paragraph
            range.setEndAfter(p);

            // Extract the contents of the paragraph after the caret into a fragment
            var contentAfterRangeStart = range.extractContents();

            // Collapse the range immediately after the paragraph
            range.collapseAfter(p);

            // Insert the content
            range.insertNode(contentAfterRangeStart);

            // Move the caret to the insertion point
            range.collapseAfter(p);
            sel.setSingleRange(range);
        }
    }
}

修改搜索包含元素的代码以搜索类名:

    while (p && (p.nodeType != 1 || p.className != "section_level1") ) {
        p = p.parentNode;
    }

关于javascript - 在插入符号位置插入嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645239/

相关文章:

javascript - 当没有指定时,CryptoJS 如何获得 IV?

javascript - 具有 WebView 和 native View 的混合应用程序的优缺点是什么?

javascript - 如何使用 JavaScript 获取 HTML Table 中 td 内的链接文本?

javascript - JQuery 加载不从缓存加载

javascript - jQuery:html 元素的内容被解释为字符串,而不是整数

jquery - 使用 css 打开和关闭动画 - 更好的动画

javascript - 如何对齐基于 DOM 居中 div 的 SVG 网格

javascript - Canvas 偏移 - 鼠标指针未同步

css - 动态调整 Div

javascript - JS - 一个大文件还是许多小文件? - 在哪里划线?