FCKeditor操作选中对象Selection API
FCKeditor Selection 选中文本
FCKeditor Selection 对象使用举例:
// The selection object
FCKeditorAPI.GetInstance("FCKeditor").Selection
// properties
FCKeditorAPI.GetInstance("FCKeditor").Selection.GetType();
FCKeditorAPI.GetInstance("FCKeditor").Selection.GetSelectedElement();
FCKeditorAPI.GetInstance("FCKeditor").Selection.GetParentElement(); // Doesn't seem to be working in Fx when the selection is only one character
// methods
// select node, e.g. select the parent node
FCKeditorAPI.GetInstance("FCKeditor").Selection.SelectNode(FCKeditorAPI.GetInstance("FCKeditor").Selection.GetParentElement());
// Move cursor position before (toStart = true, default) or after (toStart = false) the selection
FCKeditorAPI.GetInstance("FCKeditor").Selection.Collapse(toStart);
// Check if the selection has a specified ancestor node, returns true or false. The "nodeTagName" parameter must be Upper Case.
alert(FCKeditorAPI.GetInstance("FCKeditor").Selection.HasAncestorNode(nodeTagName));
// Move selection to ancestor node. The "nodeTagName" parameter must be Upper Case.
FCKeditorAPI.GetInstance("FCKeditor").Selection.MoveToAncestorNode(nodeTagName);
// Delete selection
FCKeditorAPI.GetInstance("FCKeditor").Selection.Delete();
// so to get the element that is selected, first check the type of the selection
// Doesn't seem to be working in Fx when the selection is only one character - This is due to some bug in the 'GetParentElement' method
if(FCKeditorAPI.GetInstance("FCKeditor").Selection.GetType() == 'Control')
{
alert(FCKeditorAPI.GetInstance("FCKeditor").Selection.GetSelectedElement());
}
else if(FCKeditorAPI.GetInstance("FCKeditor").Selection.GetType() == 'Text')
{
alert(FCKeditorAPI.GetInstance("FCKeditor").Selection.GetParentElement());
}
// Fetching the actual content of the selection can't be done through the 'Selection' Object...
if (document.all)
{
alert(FCKeditorAPI.GetInstance("FCKeditor").EditorDocument.selection.createRange().text);
}
else
{
alert(oSel = FCKeditorAPI.GetInstance("FCKeditor").EditorWindow.getSelection());
}
API参考:
GetParentBlock = function(){var A=this.GetParentElement();while (A){if (FCKListsLib.BlockBoundaries[A.nodeName.toLowerCase()]) break;A=A.parentNode;};return A;}
ApplyStyle = function(A){FCKStyles.ApplyStyle(new FCKStyle(A));}
GetType = function(){try{var A=FCK.EditorDocument.selection.type;if (A=='Control'||A=='Text') return A;if (FCK.EditorDocument.selection.createRange().parentElement) return 'Text';}catch(e){};return 'None';}
GetSelectedElement = function(){if (this.GetType()=='Control'){var A=FCK.EditorDocument.selection.createRange();if (A&&A.item) return FCK.EditorDocument.selection.createRange().item(0);};return null;}
GetParentElement = function(){switch (this.GetType()){case 'Control':var A=FCKSelection.GetSelectedElement();return A?A.parentElement:null;case 'None':return null;default:return FCK.EditorDocument.selection.createRange().parentElement();}}
GetBoundaryParentElement = function(A){switch (this.GetType()){case 'Control':var B=FCKSelection.GetSelectedElement();return B?B.parentElement:null;case 'None':return null;default:var C=FCK.EditorDocument;var D=C.selection.createRange();D.collapse(A!==false);var B=D.parentElement();return FCKTools.GetElementDocument(B)==C?B:null;}}
SelectNode = function(A){FCK.Focus();FCK.EditorDocument.selection.empty();var B;try{B=FCK.EditorDocument.body.createControlRange();B.addElement(A);}catch(e){B=FCK.EditorDocument.body.createTextRange();B.moveToElementText(A);};B.select();}
Collapse = function(A){FCK.Focus();if (this.GetType()=='Text'){var B=FCK.EditorDocument.selection.createRange();B.collapse(A==null||A===true);B.select();}}
HasAncestorNode = function(A){var B;if (FCK.EditorDocument.selection.type=="Control"){B=this.GetSelectedElement();}else{var C=FCK.EditorDocument.selection.createRange();B=C.parentElement();};while (B){if (B.tagName==A) return true;B=B.parentNode;};return false;}
MoveToAncestorNode = function(A){var B,oRange;if (!FCK.EditorDocument) return null;if (FCK.EditorDocument.selection.type=="Control"){oRange=FCK.EditorDocument.selection.createRange();for (i=0;i Delete = function(){var A=FCK.EditorDocument.selection;if (A.type.toLowerCase()!="none"){A.clear();};return A;} 有价值链接: http://www.fckeditor.net/forums/viewtopic.php?f=6&t=806 http://www.fckeditor.net/forums/viewtopic.php?f=6&t=11978