1. 程式人生 > >輕量型編輯器 使用wangEditor富文字編輯器

輕量型編輯器 使用wangEditor富文字編輯器

今天忽然想起來搞一下富文字編輯器的使用,本來想用百度的ueditor,但是貌似校園網給牆了。然後從知乎上發現了這個編輯器,優點是介面簡潔,文件比較清楚。缺點也顯而易見,比較簡陋,要求不是很高的話,還是可以的 :)

下載解壓完成之後,用到的只有dist這個檔案。

這裡寫圖片描述

前臺頁面程式碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>editor測試</title>
    <!--引入wangEditor.css-->
<link rel="stylesheet" type="text/css" href="../dist/css/wangEditor.min.css"> <style type="text/css"> .container{ width: 800px; height: auto; margin: 50px auto; } .container textarea{ height: 400px; } </style
>
</head> <body> <div class="container"> <div><h1></h1></div> <div> <form action="function.php" method="post"> <p> <strong>文章標題:</strong> </p> <p> <input
type="text" name="title">
</p> <p> <strong>文章內容:</strong> </p> <p> <textarea id="textarea1" name="content"></textarea> </p> <p> <input type="submit" value="提交"> </p> </form> </div> </div> <!--引入jquery和wangEditor.js--> <!--注意:javascript必須放在body最後,否則可能會出現問題--> <script type="text/javascript" src="../dist/js/lib/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="../dist/js/wangEditor.min.js"></script> <!--這裡引用jquery和wangEditor.js--> <script type="text/javascript"> var editor = new wangEditor('textarea1'); // 上傳圖片(舉例) editor.config.uploadImgUrl = '/upload.php'; editor.config.uploadImgFileName = 'myFileName'; editor.create(); </script> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

為了簡單起見,我採用了form的提交方式,action對應的function.php檔案程式碼如下:

<?php
/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/8
 * Time: 17:06
 */
echo $_REQUEST['content'];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

function.php的作用就是看富文字編輯器提交過去的資料,在實際應用中$_REQUEST[‘content’]的內容是會儲存到資料庫中的。

在前段程式碼中我們可以看到通過editor.config.uploadImgUrl = '/upload.php'; 
配置了圖片上傳的操作頁面,upload.php

<?php
/**
 * Created by PhpStorm.
 * User: koastal
 * Date: 2016/5/8
 * Time: 16:38
 */
$imgInfo = $_FILES['myFileName'];
$oldname = $imgInfo['name'];
$tmp_name = $imgInfo['tmp_name'];
$temp = explode(".",$oldname);
$newname = time().".".$temp[count($temp)-1];
move_uploaded_file($tmp_name,'upload/'.$newname);
echo $dir = "http://".getenv('HTTP_HOST')."/upload/".$newname;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

upload.php的作用是在後臺實現圖片的轉存,然後將圖片的實際地址返回。