ThinkPHP中使用Ueditor富文本编辑器
具体插件下载:
http://ueditor.baidu.com/website/download.html#ueditor
UEditor官方文档:
http://ueditor.baidu.com/website/document.html
之前于"ThinkPHP-代码"案例中发布版本:
http://www.thinkphp.cn/code/175.html
UEditor解压于:PUBLIC/Ueditor下(同级目录有:Common,Conf,Lib,Tpl等)
例:在Tpl/model/model.html:
<html>
<title>Ueditor文本编辑器</title>
<head>
<title>完整demo</title>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<loadhref="__PUBLIC__/Ueditor/ueditor.config.js"/>
<loadhref="__PUBLIC__/Ueditor/ueditor.all.min.js"/>
<!--使用版-->
<!--<scripttype="text/javascript"charset="utf-8"src="../ueditor.all.js"></script>-->
<!--开发版-->
<!--<scripttype="text/javascript"charset="utf-8"src="editor_api.js"></script>-->
<!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
<loadhref="__PUBLIC__/Ueditor/lang/zh-cn/zh-cn.js"/>
<styletype="text/css">
.clear{
clear:both;
}
</style>
</head>
<body>
<div>
<formname='MyForm'id='MyForm'method='POST'action="__URL__/message_insert">
<scriptid="editor"name="editor"type="text/plain"style="width:1024px;height:300">
从数据库中取出文章内容打印到此处!!!
</script>
</form>
</div>
<divid="btns">
<div>
<buttononclick="getAllHtml()">获得整个html的内容</button>
<buttononclick="getContent()">获得内容</button>
<buttononclick="setContent()">写入内容</button>
<buttononclick="setContent(true)">追加内容</button>
<buttononclick="getContentTxt()">获得纯文本</button>
<buttononclick="getPlainTxt()">获得带格式的纯文本</button>
<buttononclick="hasContent()">判断是否有内容</button>
<buttononclick="setFocus()">使编辑器获得焦点</button>
</div>
<div>
<buttononclick="getText()">获得当前选中的文本</button>
<buttononclick="insertHtml()">插入给定的内容</button>
<buttonid="enable"onclick="setEnabled()">可以编辑</button>
<buttononclick="setDisabled()">不可编辑</button>
<buttononclick="UE.getEditor('editor').setHide()">隐藏编辑器</button>
<buttononclick="UE.getEditor('editor').setShow()">显示编辑器</button>
<buttononclick="UE.getEditor('editor').setHeight(300)">设置编辑器的高度为300</button>
</div>
</div>
<div>
<buttononclick="createEditor()"/>
创建编辑器</button>
<buttononclick="deleteEditor()"/>
删除编辑器</button>
<buttononclick="submitEditor()"/>
提交</button>
</div>
</body>
<scripttype="text/javascript">
//UEDITOR_HOME_URL、config、all这三个顺序不能改变(绝对路径)
//window.UEDITOR_HOME_URL="/ThinkPHP/Public/Ueditor/";
//实例化编辑器
varue=UE.getEditor('editor');
functioninsertHtml(){
varvalue=prompt('插入html代码','');
ue.execCommand('insertHtml',value)
}
functioncreateEditor(){
enableBtn();
UE.getEditor('editor');
}
functiongetAllHtml(){
alert(UE.getEditor('editor').getAllHtml())
}
functiongetContent(){
vararr=[];
arr.push("使用editor.getContent()方法可以获得编辑器的内容");
arr.push("内容为:");
arr.push(UE.getEditor('editor').getContent());
alert(arr.join("\n"));
}
functiongetPlainTxt(){
vararr=[];
arr.push("使用editor.getPlainTxt()方法可以获得编辑器的带格式的纯文本内容");
arr.push("内容为:");
arr.push(UE.getEditor('editor').getPlainTxt());
alert(arr.join('\n'))
}
functionsetContent(isAppendTo){
vararr=[];
arr.push("使用editor.setContent('欢迎使用ueditor')方法可以设置编辑器的内容");
UE.getEditor('editor').setContent('欢迎使用ueditor',isAppendTo);
alert(arr.join("\n"));
}
functionsetDisabled(){
UE.getEditor('editor').setDisabled('fullscreen');
disableBtn("enable");
}
functionsetEnabled(){
UE.getEditor('editor').setEnabled();
enableBtn();
}
functiongetText(){
//当你点击按钮时编辑区域已经失去了焦点,如果直接用getText将不会得到内容,所以要在选回来,然后取得内容
varrange=UE.getEditor('editor').selection.getRange();
range.select();
vartxt=UE.getEditor('editor').selection.getText();
alert(txt)
}
functiongetContentTxt(){
vararr=[];
arr.push("使用editor.getContentTxt()方法可以获得编辑器的纯文本内容");
arr.push("编辑器的纯文本内容为:");
arr.push(UE.getEditor('editor').getContentTxt());
alert(arr.join("\n"));
}
functionhasContent(){
vararr=[];
arr.push("使用editor.hasContents()方法判断编辑器里是否有内容");
arr.push("判断结果为:");
arr.push(UE.getEditor('editor').hasContents());
alert(arr.join("\n"));
}
functionsetFocus(){
UE.getEditor('editor').focus();
}
functiondeleteEditor(){
disableBtn();
UE.getEditor('editor').destroy();
}
//提交方法
functionsubmitEditor(){
//此处以非空为例
if(ue.hasContents()){
ue.sync();//同步内容
document.MyForm.submit();
}
}
functiondisableBtn(str){
vardiv=document.getElementById('btns');
varbtns=domUtils.getElementsByTagName(div,"button");
for(vari=0,btn;btn=btns[i++];){
if(btn.id==str){
domUtils.removeAttributes(btn,["disabled"]);
}else{
btn.setAttribute("disabled","true");
}
}
}
functionenableBtn(){
vardiv=document.getElementById('btns');
varbtns=domUtils.getElementsByTagName(div,"button");
for(vari=0,btn;btn=btns[i++];){
domUtils.removeAttributes(btn,["disabled"]);
}
}
</script>