使用Js獲取、插入和更改FCKeditor編輯器里的內(nèi)容
之前在一個系統(tǒng)里使用了FCKeditor編輯器,由于項目需求需要在FCKeditor里添加一個自定義的按鈕用于實現(xiàn)自己的需求
主要是在點擊該按鈕時刪除或添加FCKeditor編輯器里的內(nèi)容
其實是一個很簡單的需求,本來以為在FCKeditor可以很容易的實現(xiàn)
在Google上搜索自定義按鈕,插件開發(fā),經(jīng)過近二個小時的摸索最終還是沒有實現(xiàn)不知是我太笨還是自定義插件太難啦
通過JS方式來處理
1.在頁面中添加checkbox元素并綁定事件,選中該元素時將在FCKeditor內(nèi)容里添加"{#book#}"字符串(該字符串會在適當?shù)臅r候被替換成其他內(nèi)容),取消選中時則刪除
<label><input type="checkbox" id="lineBook" onclick="chk_but();"/>添加/刪除復(fù)選框</label>
2.添加Js處理FCKeditor內(nèi)容(添加或刪除"{#book#}"字符串),'txtContent'為FCKeditor的ID控控件ID
<script type = "text/javascript" >
//"添加/刪除復(fù)選框"點擊時如果按鈕選中則添加"{#book#}"字符串到FCK內(nèi)容里,反之刪除字符串
//lineBook為FCK的ID號
function chk_but() {
if (window.FCKeditorAPI !== undefined && FCKeditorAPI.GetInstance('txtContent') !== undefined) {
if (document.getElementById('lineBook').checked) {
FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML += "{#book#}";
} else {
FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML = FCKeditorAPI.GetInstance('txtContent').EditorDocument.body.innerHTML.replace("{#book#}", "");
}
}
} //end function chk_lineBook()
//內(nèi)容里如果有{#book#}則選中"添加/刪除復(fù)選框"
if (document.getElementById('txtContent').value.indexOf('{#book#}') >= 0
&& window.FCKeditorAPI !== undefined
&& FCKeditorAPI.GetInstance('txtContent') !== undefined) {
document.getElementById('lineBook').checked = true;
}
</script>
參考:
官網(wǎng):http://ckeditor.com/
獲取或更改內(nèi)容值:http://bbs.csdn.net/topics/360086762
創(chuàng)建插件:http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Customization/Plug-ins
接著給大家分享一下JS操作Fckeditor的一些常用方法
//向編輯器插入指定代碼
function insertHTMLToEditor(codeStr){
var oEditor = FCKeditorAPI.GetInstance("content");
oEditor.InsertHtml(codeStr); // "html"為HTML文本
}
//獲取編輯器中HTML內(nèi)容
function getEditorHTMLContents() {
var oEditor = FCKeditorAPI.GetInstance("content");
return(oEditor.GetXHTML(false));
}
// 獲取編輯器中文字內(nèi)容
function getEditorTextContents() {
var oEditor = FCKeditorAPI.GetInstance("content");
return(oEditor.EditorDocument.body.innerText);
}
// 設(shè)置編輯器中內(nèi)容
function SetEditorContents(ContentStr) {
var oEditor = FCKeditorAPI.GetInstance("content") ;
oEditor.SetHTML(ContentStr) ;
}
//向編輯器插入指定代碼
function insertHTMLToEditor(codeStr){
var oEditor = FCKeditorAPI.GetInstance( "content ");
if (oEditor.EditMode==FCK_EDITMODE_WYSIWYG){
oEditor.InsertHtml(codeStr);
}else{
return false;
}
}
//統(tǒng)計編輯器中內(nèi)容的字數(shù)
function getLength(){
var oEditor = FCKeditorAPI.GetInstance( "content ");
var oDOM = oEditor.EditorDocument;
var iLength ;
if(document.all){
iLength = oDOM.body.innerText.length;
}else{
var r = oDOM.createRange();
r.selectNodeContents(oDOM.body);
iLength = r.toString().length;
}
alert(iLength);
}
//執(zhí)行指定動作
function ExecuteCommand(commandName){
var oEditor = FCKeditorAPI.GetInstance( "content ") ;
oEditor.Commands.GetCommand(commandName).Execute() ;
}
到此這篇關(guān)于使用Js獲取、插入和更改FCKeditor編輯器里的內(nèi)容的文章就介紹到這了,更多相關(guān)Js操作FCKeditor編輯器內(nèi)容請搜素腳本之家以前的文章或下面相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
SyntaxHighlighter 去掉右側(cè)滾動條的方法
SyntaxHighlighter這個是一個高亮插件?,F(xiàn)在被用于很多網(wǎng)站的代碼顯示。但是SyntaxHighlighter3.0.83,由于自適應(yīng)寬和高,導致一直有滾動條的問題2020-03-03
SyntaxHighlighter配合CKEditor插件輕松打造代碼語法著色
作為程序員在寫博客文章的時候,經(jīng)常要些代碼片斷,很多博客系統(tǒng)都提供代碼語法著色高亮顯示的功能或插件,讓代碼顯示更直接明了2012-09-09
徹底解決ewebeditor網(wǎng)站后臺不能上傳圖片的方法
隨著windows操作系統(tǒng)和IE瀏覽器版本的不斷推出,很多客戶都漸漸放棄了IE6,IE7,使用上了IE8瀏覽器,但是突然發(fā)現(xiàn)自己網(wǎng)站后臺里面的eWebEditor文本編輯器的插入圖片等等按鈕失效了,鼠標點擊后什么反應(yīng)都沒有,IE瀏覽器左下角顯示一個黃色的感嘆號提示錯誤,就以為是網(wǎng)站空間、服務(wù)器中毒了呢,一時間紛紛都來找我們詢問2012-03-03
javascript 網(wǎng)頁編輯框及拖拽圖片的問題
javascript 網(wǎng)頁編輯框及拖拽圖片的問題,需要的朋友可以參考下。2009-12-12
FCKeditor 2.6.5 ASP環(huán)境安裝配置使用說明
今天用到在線編輯器在asp環(huán)境下上傳竟然無效。找了好久才找到這介紹,現(xiàn)備份于此,需要的朋友可以參考下2012-03-03
解密FCKeditor 2.0 的設(shè)置.修改.使用方法
解密FCKeditor 2.0 的設(shè)置.修改.使用方法...2007-11-11

