Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

javascript - Rangy (JS/jQuery) split node

How would I split a node/element at a position (selection).

Example I have this markup:

<p>This is <a href="">a te|st</a>, you like?</p>

(this pipe represents the position/selection)

I want to convert it to:

<p>This is <a href="">a te</a></p>|<p><a href="">st</a>, you like?</p>

Maintaining the selection.

Any ideas?

I and using the Rangy library, and also jQuery, but can use raw JS if applicable.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could do this by creating a range that extends from the caret to the point immediately after the paragraph and using its extractContents() method.

Live demo: http://jsfiddle.net/timdown/rr9qs/2/

Code:

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...