百度编辑器Ueditor的使用
使用效果截图:
显示表情:
上传图片:
代码示例:
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" validateRequest="false " %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Ueditor测试</title> <script src="ueditor/editor_config.js" type="text/javascript"></script> <script src="ueditor/editor_all_min.js" type="text/javascript"></script> <link href="ueditor/themes/default/ueditor.css" rel="stylesheet" type="text/css"/> </head> <body> <form id="form1" runat="server"> <div id="editorValuedata" style="display: none;"> <%=content%><!--取得后台初始值--> </div> <div id="editorValue"> </div> <asp:Button ID="btnSubmit" runat="server" Text="点击我一下告诉你提交的内容" OnClick="btnSubmit_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </form> </body> <script type="text/javascript"> var editor =new baidu.editor.ui.Editor({//实例化编辑器 UEDITOR_HOME_URL:'ueditor/', iframeCssUrl: 'ueditor/themes/default/iframe.css' }); editor.render('editorValue'); //将编译器渲染到容器 editor.setContent(document.getElementById('editorValuedata').innerHTML); //设置初始值,你可以将初始值放到<div id="editorValuedata" style="display:none"></div>内 document.getElementById('editorValuedata').innerHTML =""; </script> </html>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { content = "进入信息";//设置初始值 } }
第一步:先配置UEditor
< script src = "ueditor/ueditor.config.js" ></ script > < script src = "ueditor/ueditor.all.min.js" ></ script > < script type = "text/javascript" > var editor = UE.getEditor('myEditor', { initialFrameWidth: 800, initialFrameHeight: 300, }); </ script > |
第二步:放置编辑器
< script type = "text/plain" id = "myEditor" ></ script > |
第三步:以上已经把UEditor部署好了,下面实现单独使用图片和文件上传
<script type= "text/javascript" > //重新实例化一个编辑器,防止在上面的editor编辑器中显示上传的图片或者文件 var _editor = UE.getEditor( 'upload_ue' ); _editor.ready( function () { //设置编辑器不可用 _editor.setDisabled(); //隐藏编辑器,因为不会用到这个编辑器实例,所以要隐藏 _editor.hide(); //侦听图片上传 _editor.addListener( 'beforeInsertImage' , function (t, arg) { //将地址赋值给相应的input $( "#picture" ).attr( "value" , arg[0].src); //图片预览 $( "#preview" ).attr( "src" , arg[0].src); }) //侦听文件上传 _editor.addListener( 'afterUpfile' , function (t, arg) { $( "#file" ).attr( "value" , _editor.options.filePath + arg[0].url); }) }); //弹出图片上传的对话框 function upImage() { var myImage = _editor.getDialog( "insertimage" ); myImage.open(); } //弹出文件上传的对话框 function upFiles() { var myFiles = _editor.getDialog( "attachment" ); myFiles.open(); } </script> |
第四步:最后一步,对文件上传非常重要,在ueditor文件夹中找到文件dialogs\attachment\attachment.html中找到代码editor.execCommand("insertHTML",str);在上面添加下面的代码
editor.fireEvent('afterUpfile', filesList); |
然后在<body></body>中随便找个位置放置这个编辑器
< script type = "text/plain" id = "upload_ue" ></ script > |
说明:可以把两次初始化编辑的代码合并一起放到文件最后的</body>前面,图片上传的配置就不说了,附件中例子已经配置好了,下载下来参考一下就行。转载请标明出处。
转载请注明本文链接:http://www.holdcode.com/article/home/details/23