简体   繁体   中英

CKEDITOR :: Get selected text's parent node

I am working in CKEditor right now.And I have a Question for You guys.

I am Selecting particular text in CKEditor's Text area and getting the selected

nodes HTML content Using the Below code.

 var editor = CKEDITOR.instances.editor1;
 var sel = editor.getSelection();
 sel.selectElement(sel.getStartElement());
 var ranges = sel.getRanges();
 var el = new CKEDITOR.dom.element("div");

   for (var i = 0, len = ranges.length; i < len; ++i) {
             el.append(ranges[i].cloneContents());
                  }
  alert(el.getHtml());

The following returuns the currently selected text HTML content.

alert(el.getHtml());

My Question is How I get the selected node's parent tag?

Example

Example Word is,

<p>hi<b>welcome</b>world<p>

My Selection is,

<b>welcome</b>

How do i get the below parent tag.

<p></p>

i find the answer.

//Get range
range = xhtmlCKEditor.createRange();

range.setStart(anchors[0].tag , 0 );
range.setEnd(anchors[0].tag.getLast(), 1 );

var firstNode = range.startContainer.getParent();
var lastNode = range.endContainer.getParent();
if(lastNode.type === CKEDITOR.NODE_ELEMENT && lastNode.getName() === "span")
{
    range.setEndAfter(lastNode);

}

//Make end Get full if is tcElement
if(firstNode.type === CKEDITOR.NODE_ELEMENT && firstNode.getName() === "p")
{
    range.setStartBefore(firstNode);
}
xhtmlCKEditor.getSelection().selectRanges([range]);

Then print the Selection it will get the html With parent tag .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM