thinkphp 编辑器kindeditor
??首先,去官網(wǎng)下載最新版的kindeditor,然后把里面asp,jsp,net,example的全刪除,然后改名為editor放進(jìn)public(最外層目錄的public)文件夾里面
?????在目錄lib目錄建立ORG文件夾(個(gè)人習(xí)慣用ORG存儲(chǔ)公用類),建立一個(gè)共用類,editor.class.php
下面是這個(gè)類的具體代碼???
<?php
/*編輯器調(diào)用的初始化類
?*
?*/
class?editor?{
var?$Width;
var?$Height;
var?$Value;
/*?此方法是編輯器的構(gòu)造方法
?*第一個(gè)參數(shù),$Height是高度,不填默認(rèn)是500px
?*第二個(gè)參數(shù),$Width是寬度,不填默認(rèn)是700px
?*第三個(gè)參數(shù),$Value是編輯器默認(rèn)內(nèi)容,不填默認(rèn)是“<h2>歡迎使用編輯器</h2><br>”
?*
?*/
function?editor($Height="500px",$Width="700px",$Value="<h2>歡迎使用編輯器</h2><br>")?{
$this->Value?=?$Value;
$this->Height?=?$Height;
$this->Width?=?$Width;
}
?
?
/*此方法是在線編輯器的調(diào)用
?*?在需要編輯器的地方調(diào)用此函數(shù)
?*/
function?createEditor(){
return?"<textarea?name='content1'?style='width:$this->Width;height:$this->Height;visibility:hidden;'>$this->Value</textarea>";
}
?
/*使用在線編輯器必須在html<head></head>之間調(diào)用此方法,才能正確調(diào)用,
?*?內(nèi)容主要都是script
?*/
function?usejs(){
$str=<<<eot
<link?rel="stylesheet"?href="__PUBLIC__/editor/themes/default/default.css"?/>
<link?rel="stylesheet"?href="__PUBLIC__/editor/plugins/code/prettify.css"?/>
<script?charset="utf-8"?src="__PUBLIC__/editor/kindeditor.js"></script>
<script?charset="utf-8"?src="__PUBLIC__/editor/lang/zh_CN.js"></script>
<script?charset="utf-8"?src="__PUBLIC__/editor/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K)?{
var?editor1?=?K.create('textarea[name="content1"]',?{
cssPath?:?'__PUBLIC__/editor/plugins/code/prettify.css',
uploadJson?:?'__PUBLIC__/editor/php/upload_json.php',
fileManagerJson?:?'__PUBLIC__/editor/php/file_manager_json.php',
allowFileManager?:?true,
afterCreate?:?function()?{
var?self?=?this;
K.ctrl(document,?13,?function()?{
self.sync();
K('form[name=example]')[0].submit();
});
K.ctrl(self.edit.doc,?13,?function()?{
self.sync();
K('form[name=example]')[0].submit();
});
}
});
prettyPrint();
});
</script>
eot;
return?$str;
}
?
/*取得在線編輯器的值并返回
?*/
?function?getEditorContent(){
????$htmlData?=?'';
???if?(!empty($_POST['content1']))?{
if?(get_magic_quotes_gpc())?{
$htmlData?=?stripslashes($_POST['content1']);
}?else?{
$htmlData?=?$_POST['content1'];
}
return?$htmlData;
???}
?}
?
}
代碼注釋都寫的比較清楚了,然后在action建立個(gè)文件,是IndexAction.class.php
<?php
class?IndexAction?extends?Action?{
public?function?_initialize()?{???????
header("Content-Type:text/html;?charset=utf-8");
}
?
????public?function?index(){
?????import("@.ORG.editor");??//導(dǎo)入類
????$editor=new?editor();?????//創(chuàng)建一個(gè)對象
$a=$editor->createEditor();???//返回編輯器
$b=$editor->usejs();?????????????//js代碼
$this->assign('usejs',$b);?????//輸出到html
????????$this->assign('editor',$a);
????????$this->display();??????
?
????}
????public?function?php(){
????import("@.ORG.editor");
????$editor=new?editor();???
????$a=$editor->getEditorContent();???//獲取編輯器的內(nèi)容
$this->assign('a',$a);
$this->display();
//?$this->success('數(shù)據(jù)添加成功!');
????}
}
?
然后在tpl建立index文件夾,在里面建立2個(gè)html文件,
index.html????//使用編輯器
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
<title>Insert?title?here</title>
???{$usejs}
</head>
<body>
<form?name="example"?method="post"?action="__URL__/php">
<?php?//<textarea?name="content1"?style="width:700px;height:200px;visibility:hidden;"></textarea>????>
????????{$editor}
<br?/>
<input?type="submit"?name="button"?value="提交內(nèi)容"?/>?(提交快捷鍵:?Ctrl?+?Enter)
</form>
</body>
</html>
?
php.html????//獲取編輯器的內(nèi)容
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
<title>Insert?title?here</title>
</head>
<body>
{$a}
</body>
</html>
總結(jié)
以上是生活随笔為你收集整理的thinkphp 编辑器kindeditor的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse安装ADT失败解决办法
- 下一篇: vue跨域解决方案websocket_前