Finding Touched elments CKEDITOR

隐身守侯 提交于 2019-12-12 02:55:09

问题


I am looking several days now on the web, but i can't seem to find out, how i can find if the caret is touching a element. Let me explain myself a little better with this example.

This is a[ ] <span>example</span>

As Example we say that the caret is the tag [ ]. And now we like to know if the caret is near a span element? How can we find this? Also I need to know if the span element is before or after the caret.

I'm doing this with the CKEDITOR 4.0 api..


回答1:


Try this:

var range = CKEDITOR.instances.yourEditorInstance.getSelection().getRanges()[ 0 ];
range.optimize();
console.log( range.getBoundaryNodes() );

Also:

console.log( range.getTouchedEndNode() );
console.log( range.getTouchedStartNode() );

Note that your range may be located inside of an element (like e4, image below), so boundary nodes will always be text nodes. Check DOM range specification for more.


EDIT: These are also worth trying:

range.getNextNode();
range.getPreviousNode();

After all, once you have the node, you can check it:

var node = range.getNextNode();
console.log( node.type == CKEDITOR.NODE_ELEMENT && node.is( 'span' ) );


来源:https://stackoverflow.com/questions/15084648/finding-touched-elments-ckeditor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!