使用百度的富文本編輯器UEditor遇到的問題總結


1、下載地址:http://ueditor.baidu.com/website/download.html(我下載的是.net版本)

下載后要先看一下ueditor.config.js和 net/config.json的文件,里面是關於圖片上傳和其他方法的一些路徑配置

2、顯示編輯頁面(一定要看使用文檔:http://fex.baidu.com/ueditor/#server-deploy)

  1. 引入所需的js文件
  2. 初始化編輯器    html代碼:
    1 <div class="form-group has-warning">
    2                 <textarea class="A4page" id="myEditor" name="NewsContent"></textarea>
    3             </div>

    jquery代碼:

     1  var ue = UE.getEditor('myEditor', {
     2             toolbars: [
     3                 ['fullscreen', 'source', '|', 'undo', 'redo', '|',
     4             'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
     5             'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
     6             'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
     7             'directionalityltr', 'directionalityrtl', 'indent', '|',
     8             'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
     9             'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
    10             'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
    11             'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
    12             'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
    13             'print', 'preview', 'searchreplace', 'help', 'drafts']
    14             ],
    15             allHtmlEnabled: false,//提交到后台的數據是否包含整個html字符串
    16             autoHeightEnabled: false,
    17             autoFloatEnabled: true,
    18             allowDivTransToP: false//阻止div標簽自動轉換為p標簽
    19         });

    說明:修改配置項的方法: 1. 方法一:修改 ueditor.config.js 里面的 toolbars 2. 方法二:實例化編輯器的時候傳入 toolbars 參數(詳情參考:http://fex.baidu.com/ueditor/#start-toolbar)

  3. 前端配置基本上配置好了,下面是后端配置(如果后端配置不好,上傳圖片功能將無法使用。網址:http://fex.baidu.com/ueditor/#server-net)我配置了好久才配置好的,按照文檔上說的做就行了
  4. 當你的編輯器可以使用的時候,獲取編輯器里的內容(網址:http://fex.baidu.com/ueditor/#api-common)
  5.  1 //實例化編輯器到id為 container 的 dom 容器上:
     2 var ue = UE.getEditor('container');
     3 //設置編輯器內容:
     4 ue.ready(function() {
     5     ue.setContent('<p>hello!</p>');
     6 });
     7 //追加編輯器內容:
     8 ue.ready(function() {
     9     ue.setContent('<p>new text</p>', true);
    10 });
    11 //獲取編輯器html內容:
    12 ue.ready(function() {
    13     var html = ue.getContent();
    14 });
    15 //獲取純文本內容:
    16 ue.getContentTxt();
    17 //獲取保留格式的文本內容:
    18 ue.getPlainTxt();
    19 //獲取純文本內容:
    20 ue.getContentTxt();
    21 //判斷編輯器是否有內容:
    22 ue.hasContents();
    23 //讓編輯器獲得焦點:
    24 ue.focus();
    25 //讓編輯器獲得焦點
    26 ue.blur();
    27 //判斷編輯器是否獲得焦點:
    28 ue.isFocus();
    29 //設置當前編輯區域不可編輯:
    30 ue.setDisabled();
    31 //設置當前編輯區域可以編輯:
    32 ue.setEnabled();
    33 //隱藏編輯器:
    34 ue.setHide();
    35 //顯示編輯器:
    36 ue.setShow();
    37 //獲得當前選中的文本:
    38 ue.selection.getText();
    39 //常用命令:
    40 //在當前光標位置插入html內容
    41 ue.execCommand('inserthtml', '<span>hello!</span>');
    42 //設置當前選區文本格式:
    43 ue.execCommand('bold'); //加粗
    44 ue.execCommand('italic'); //加斜線
    45 ue.execCommand('subscript'); //設置上標
    46 ue.execCommand('supscript'); //設置下標
    47 ue.execCommand('forecolor', '#FF0000'); //設置字體顏色
    48 ue.execCommand('backcolor', '#0000FF'); //設置字體背景顏色
    49 //回退編輯器內容:
    50 ue.execCommand('undo');
    51 //撤銷回退編輯器內容:
    52 ue.execCommand('redo');
    53 //切換源碼和可視化編輯模式:
    54 ue.execCommand('source');
    55 //選中所有內容:
    56 ue.execCommand('selectall');
    57 //清空內容:
    58 ue.execCommand('cleardoc');
    59 //讀取草稿箱
    60 ue.execCommand('drafts');
    61 //清空草稿箱
    62 ue.execCommand('clearlocaldata');

     

  6. 輸入的數據是這樣的:
  7. 獲取到的數據是這樣的:<p><img src="http://img.baidu.com/hi/jx2/j_0015.gif"/><img src="/assets/utf8-net/net/upload/image/20160427/6359737678078530983400002.jpg" title="QQ截圖20160427144629.jpg" alt="QQ截圖20160427144629.jpg"/>防火防盜發貨的發貨</p>

  8. 將數據存到數據庫中了,那么怎么讓數據原樣顯示到編輯器里呢?(這個問題我試了一天,開始我以為是這個函數uParse,但是我試了好多路徑都不管用,如果誰用這個方法實現了,一定要告訴我一聲哦,先qqq了)
  9. 后台獲取數據:
    1  public ActionResult GetDetails(string NewsId)
    2         {
    3             int id = Int32.Parse(NewsId);
    4             NewsInfo news = db.NewsInfoes.Find(id);
    5             ViewData["news"] = news;
    6             return View();
    7         }

    前台html代碼:

     1 <div class="col-sm-6">
     2     <div class="panel">
     3         <div class="panel-body">
     4             <div class="form-group has-success">
     5                 <label class="control-label" for="NewsName">新聞標題:</label>
     6                 <input type="text" class="form-control" id="NewsName" name="NewsName" placeholder="新聞標題" style="width:600px" value="@(((NewsInfo)ViewData["news"]).NewsName)" />
     7             </div>
     8             <div class="form-group has-warning">
     9                 <textarea class="A4page" id="myEditor" name="NewsContent">@(((NewsInfo)ViewData["news"]).NewsContent)</textarea>
    10             </div>
    11         </div>
    12     </div>
    13 </div>

    jquery代碼:

     1    function load() {
     2             var ue = UE.getEditor('myEditor', {
     3                 toolbars: [
     4                     ['fullscreen', 'source', '|', 'undo', 'redo', '|',
     5                 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
     6                 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
     7                 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
     8                 'directionalityltr', 'directionalityrtl', 'indent', '|',
     9                 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
    10                 'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
    11                 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertframe', 'insertcode', 'pagebreak', 'template', 'background', '|',
    12                 'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
    13                 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
    14                 'print', 'preview', 'searchreplace', 'help', 'drafts']
    15                 ],
    16                 autoHeightEnabled: false,
    17                 autoFloatEnabled: true,
    18                 allowDivTransToP: false//阻止div標簽自動轉換為p標簽
    19             });
    20         }  
    21 
    22  $(function () {
    23             load();
    24         });

    然后就可以了,O(∩_∩)O哈哈~

  10. 我的流程:先添加數據
  11. 在新聞列表里點擊新聞標題,傳遞新聞id獲取新聞詳細內容
  12. 最后將數據展示到新聞修改頁面的編輯器上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM