這篇文章主要介紹了使用(yong)Js獲取、使??用插(′;д;`)入和更改FCKeditor(′?_?`)編輯器里的獲取和更內容,包括JS操作Fckeditor的插入一些常用方法,需要的朋友可以參考下
之前在一個(gè)??系統里使用了FCKeditor編輯器,由于項目需求需要在FCKeditor里添加??一個(gè)自定??義的編輯按鈕用于實(shí)現自己的需求
主要是在點(diǎn)擊該按鈕時(shí)刪除或添??加FCKeditor編輯器里的內容
其實(shí)是一個(gè)(′▽?zhuān)?很簡(jiǎn)單的需求,本來(lái)以為在FCKeditor可以很容易的器里實(shí)現
在Google上搜索自定義按鈕,插件開(kāi)發(fā),經(jīng)過(guò)近二個(gè)小時(shí)的使用┐(′?`)┌摸索最終還是沒(méi)有實(shí)現不知是我太笨還是自定(′?_?`)義插件太難啦
通過(guò)JS方式來(lái)處理
1.在頁(yè)面中添加checkbox元素并綁定事件?,選中該元素時(shí)將在FCKeditor內容里添加"{ #book#}"字符串(該字符串會(huì )在適當的時(shí)候被替換成其他內容),取消選中時(shí)則刪除
添加/刪除復選框
<script typヽ(′ー`)ノe = "text/javascript" >
//"添加/刪除復選框"點(diǎn)擊??時(shí)如果按鈕選中則添加"{ #book#}"字符串到FCK內容里,反之刪除字符串
//lineBook為FCK的獲取和更ID號
fu??n(′?`)ctio???n chk_but() {
if (window.FCKeditorAPI !== undef??ined && FCKeditorAPI.GetInstance('txtContent') !== undefined) {
if (document.getElementById('lineBook').checked='checked') {
FCKeditorAPI.GetInstanc??e('txtCon??tent').EditorDocument.body.innerHTML += "{ #book#}";
} else {
FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTMLヾ(′▽?zhuān)?? = FCKeditorAPI.G(′Д` )etInstance??('txtContent').EditorDocument.body.innerHTML.replace("{ #book#}", "");??
}
}
} //??end function chk_lineBook()
//內容里如果有{ #book#}則選中"添加/刪除復選框"
if (document.getElementById('txtContent').value.indexOf('{ #book#}') >= 0
&& window.FCKeditorAPI !== undefined
&& FCKeditorAPI.GetInstance('txtContent') !== undefined) {
document.getElementBy(╬?益?)Id('lineBook').checked = true;
}
<??;/script>
參考:
官網(wǎng):http://ckeditor.com/
獲取或更改內容值:httpヾ(′ω`)?://bbs.csdn.net/topics/360086762
創(chuàng )建插件(′_`):http://(′▽?zhuān)?)docs.cksource.com/FCKeditor_2.x/Developers_Guide/Customization/Plug-ins
接著(zhù)給大家分享( ?° ?? ?°)一下JS操作Fckeditor的一些常用方法
//向編輯器(qi)插入指定代碼
function insertHTMLToEditヽ(′▽?zhuān)?ノor(codeStr){
var oEditor = FCKeditorAPI.GetInstance("content");
oEditor.InsertHtml(codeStr); // "html"為HTML文本
}
//??獲取編輯器中HTML內容
function getEditorHTMLContents() {
var oEditor = FCKeditorAPI.Get(′?ω?`)Instヾ(′?`)?ance("content");
return(oEditor.GetXHTML(fals??e));
}
// 獲取編輯器中文字內容
fu??nction getEditorTextContents() {
var oEditor = FCKeditorAPI.GetInstance("content");
return(oEditor.EditorDocument.body.innerTeヽ(′ー`)ノxt);
}
// 設置編輯器中內( ???)容
function(′?`*) SetEditorContents(ContentStr) {
var oEditor = FC??KeditorAPI.GetInstance("content") ;
oEditor.SetHTML(ContentStr) ;
}
//向編輯器插入指定代碼
function?? insertHTMLToEditor(codeStr){
var oEdi(′ω`)tor = FCKeditorAPI.GetInstance( "content ");
if (oEditor.EditMode==FCK_EDITMODE_WYSIWYG){
oEditor.InsertHtml(codeStr);
}else{
return false;
}
}
//統計編輯器中內容的字數
function getLength(){
var oEditor(′?`*) = FCKeditorAPI.GetInstance( "content ");
var oDOM = oEditor.Ed??itorDocument;
var iLength ;
if(document.all){
iLength = oDOM(′ω`).body.innerText.length;
}else{
var r = oDOM.createRange();
r.selectNodeContent??s(oDOM.body);
iLeng??th = r.toString(??).length;
}
alert(iLength);
}
//執行指定動(dòng)作
function ExecuteCommand(commandName){
var oEditor = FCKeditorAPI.GetInstance( "content ") ;
oEditor(??-)?.Commands.GetCommanヽ(′ー`)ノd(commandNa??me).Execute() ;
}
本文來(lái)自腳本之家,原文鏈接:https://www.jb51.net/article/180847.htm
插入