FCKEditor常用Js代码,获取FCK内容,统计FCK字数,向FC

网络编程 2025-03-12 22:38www.168986.cn编程入门

驾驭FCKEditor:常用的JavaScript代码与实用功能指南

一、获取编辑器内容

想要获取FCKEditor中的格式化内容,可以使用以下代码:

```javascript

function getEditorContents(){

var oEditor = FCKeditorAPI.GetInstance("content"); //获取名为“content”的编辑器实例

alert(oEditor.GetXHTML(true)); //获取编辑器的XHTML内容并弹窗显示

}

```

```javascript

function insertHTMLToEditor(codeStr){

var oEditor = FCKeditorAPI.GetInstance("content"); //获取编辑器实例

if (oEditor.EditMode==FCK_EDITMODE_WYSIWYG){ //确保编辑器处于所见即所得模式

}else{

return false; //若不在WYSIWYG模式,则返回false

}

}

```

三、统计编辑器内容字数

想要统计编辑器中的字数,可以使用以下函数:

```javascript

function getLength(){

var oEditor = FCKeditorAPI.GetInstance("content"); //获取编辑器实例

var oDOM = oEditor.EditorDocument; //获取编辑器的DOM对象

var iLength ;

if(document.all){ //针对IE浏览器

iLength = oDOM.bodynerText.length; //统计文本长度

}else{ //针对其他浏览器

var r = oDOM.createRange();

r.selectNodeContents(oDOM.body);

iLength = r.toString().length; //通过范围对象获取文本长度

}

alert(iLength); //弹窗显示字数

}

```

四、执行指定动作与设置编辑器内容

除了上述功能,您还可以使用以下函数执行编辑器的指定动作和设置编辑器内容:

```javascript

//执行指定动作,如加粗、斜体等

function ExecuteCommand(mandName){

var oEditor = FCKeditorAPI.GetInstance("content");

oEditormands.GetCommand(mandName).Execute();

}

//设置编辑器内容

function SetContents(codeStr){

var oEditor = FCKeditorAPI.GetInstance("content");

oEditor.SetHTML(codeStr); //设置编辑器的HTML内容

}

```

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by