富文本
CKEditor 4
1.下载文件https://ckeditor.com/ckeditor-4/download/
2.选择完整版
3.解压文件放在项目目录
4.配置工具栏
找到index.html打开页面
编辑自己的工具栏
根据需求选择想要的工具栏选项,获取配置代码
复制配置代码
粘贴到配置文件
5. html页面使用富文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="richText">
<button onclick="getData()">获取内容</button>
<div id="Text"></div>
</div>
<!-- 引入富文本 -->
<script src="./ckeditor/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
CKEDITOR.replace('Text');
function getData() {
<!-- 获取富文本的内容 -->
var CHtml = CKEDITOR.instances.Text.getData();
console.log(CHtml);
}
</script>
</body>
</html>
6.富文本内容编辑
//获取富文本内容
CKEDITOR.instances.Text.getData();
//设置富文本内容
CKEDITOR.instances.Text.setData();
7. 修改富文本背景
在config.js文件中添加以下代码
CKEDITOR.addCss(".cke_editable{background-color: #FFF5D3}")
config.js文件内容如下:
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see https://ckeditor.com/legal/ckeditor-oss-license
*/
CKEDITOR.editorConfig = function (config) {
config.toolbarGroups = [
{
name: 'document', groups: ['mode', 'document', 'doctools'] },
{
name: 'clipboard', groups: ['clipboard', 'undo'] },
{
name: 'editing', groups: ['find', 'selection', 'spellchecker', 'editing'] },
{
name: 'forms', groups: ['forms'] },
'/',
{
name: 'basicstyles', groups: ['basicstyles', 'cleanup'] },
{
name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi', 'paragraph'] },
{
name: 'links', groups: ['links'] },
{
name: 'insert', groups: ['insert'] },
'/',
{
name: 'styles', groups: ['styles'] },
{
name: 'colors', groups: ['colors'] },
{
name: 'tools', groups: ['tools'] },
{
name: 'others', groups: ['others'] },
{
name: 'about', groups: ['about'] }
];
config.removeButtons = 'Save,Print';
// 背景颜色
CKEDITOR.addCss(".cke_editable{background-color: #FFF5D3}")
};
8. 修改富文本默认样式
在content.css这个文件中修改想要的样式保存即可。
9. 修改富文本只读属性
// Text是页面标签的id,true为只读,false为可编辑
CKEDITOR.instances['Text'].setReadOnly(false);
页面效果如图: