news 2026/6/14 15:27:00

我网站的第一个富文本编辑器示例代码

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
我网站的第一个富文本编辑器示例代码

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>开源富文本编辑器示例</title> <!-- 引入 TinyMCE 开源版 CDN --> <script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/7/tinymce.min.js" referrerpolicy="origin"></script> <style> /* 简单的页面样式,让编辑器布局更美观 */ body { font-family: Arial, sans-serif; max-width: 1000px; margin: 20px auto; padding: 0 20px; } .editor-container { margin-top: 20px; } h1 { color: #333; text-align: center; } #submit-btn { margin-top: 15px; padding: 8px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } #submit-btn:hover { background-color: #45a049; } #content-preview { margin-top: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 4px; min-height: 100px; } </style> </head> <body> <h1>开源富文本编辑器示例</h1> <div class="editor-container"> <!-- 富文本编辑器容器 --> <textarea id="myeditor"></textarea> <!-- 提交按钮,用于演示获取编辑器内容 --> <button id="submit-btn">获取编辑内容</button> <!-- 内容预览区域 --> <div id="content-preview"> 点击上方按钮可查看编辑的内容 </div> </div> <script> // 初始化 TinyMCE 编辑器 tinymce.init({ // 绑定到 id 为 myeditor 的文本域 selector: '#myeditor', // 语言设置为中文 language: 'zh_CN', // 编辑器高度 height: 400, // 菜单栏(按需开启/关闭) menubar: 'file edit insert view format table tools', // 工具栏(基础功能) toolbar: [ 'undo redo | bold italic underline strikethrough | fontfamily fontsize | forecolor backcolor', 'alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist |', 'link unlink image | blockquote hr | removeformat | preview fullscreen' ], // 允许的内容格式(增强兼容性) extended_valid_elements: 'img[src|alt|title|width|height|style]', // 禁用自动保存(避免新手困惑) autosave_ask_before_unload: false, // 本地图片上传(基础版仅支持 base64 格式) images_upload_handler: function (blobInfo, success, failure) { // 将图片转为 base64 字符串(无需后端) success('data:image/jpeg;base64,' + blobInfo.base64()); }, // 初始化完成后的回调 setup: function (editor) { editor.on('init', function () { console.log('编辑器初始化完成'); }); } }); // 绑定按钮点击事件,获取编辑器内容并预览 document.getElementById('submit-btn').addEventListener('click', function () { // 获取编辑器的 HTML 内容 const content = tinymce.get('myeditor').getContent(); // 将内容显示到预览区域 document.getElementById('content-preview').innerHTML = content; // 控制台也输出一份(方便调试) console.log('编辑器内容:', content); }); </script> </body> </html>

需要注册一个api key,但是很简单,1分钟就能注册,随便填。

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>富文本编辑器</title> <script src="https://cdn.tiny.cloud/1/xyea5j0c2onwtobabebwjx6wkv7o2ycidg76o6bctf8mus95/tinymce/7/tinymce.min.js" referrerpolicy="origin"></script> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: "Microsoft Yahei", Arial, sans-serif; max-width: none; margin: 20px auto; padding: 0 20px; background: #fff; } .editor-container { margin-top: 20px; width: 100%; } h1 { color: #333; text-align: center; margin-bottom: 20px; } #submit-btn { margin-top: 15px; padding: 8px 20px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; } #content-preview { margin-top: 15px; padding: 15px; border: 1px solid #ddd; border-radius: 4px; min-height: 100px; width: 100%; box-sizing: border-box; } /* 核心:强制工具栏一行+靠左对齐 */ .tox-editor-container { width: 100% !important; } /* 工具栏容器靠左,取消自动换行,溢出滚动 */ .tox-toolbar__primary { width: 100% !important; display: flex !important; flex-wrap: nowrap !important; /* 禁止换行 */ justify-content: flex-start !important; /* 靠左对齐 */ overflow-x: auto !important; /* 超出部分横向滚动 */ overflow-y: hidden !important; } /* 去掉按钮分组的多余间距,紧凑显示 */ .tox-toolbar__group { margin: 0 !important; padding: 0 !important; display: flex !important; justify-content: flex-start !important; } /* 隐藏滚动条(可选,保留也可以) */ .tox-toolbar__primary::-webkit-scrollbar { height: 4px; } .tox-toolbar__primary::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; } </style> </head> <body> <h1>富文本编辑器</h1> <div class="editor-container"> <textarea id="myeditor"></textarea> <button id="submit-btn">获取编辑内容</button> <div id="content-preview">点击上方按钮可查看编辑的内容</div> </div> <script> tinymce.init({ selector: '#myeditor', language: 'zh_CN', height: 700, statusbar: false, // 隐藏底部状态栏 menubar: false, // 隐藏菜单栏(进一步精简) // 核心:所有功能按钮整合为一行,保留核心功能 toolbar: [ 'undo redo | bold italic underline strikethrough | fontsize | forecolor backcolor | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | link unlink image | blockquote hr code | removeformat preview fullscreen' ], // 关键配置:禁用工具栏换行/折叠,强制一行 toolbar_mode: 'sliding', // 滑动模式,超出宽度显示滚动/折叠按钮 toolbar_location: 'top', // 工具栏在顶部(默认) toolbar_sticky: true, // 滚动时工具栏固定(可选,提升体验) // 精简按钮样式,减少占用空间 toolbar_groups: { align: { icon: 'align', items: 'alignleft aligncenter alignright alignjustify' }, list: { icon: 'list', items: 'numlist bullist outdent indent' }, color: { icon: 'color', items: 'forecolor backcolor' } }, // 其他基础配置 extended_valid_elements: 'img[src|alt|title|width|height]', autosave_ask_before_unload: false, images_upload_handler: function (blobInfo, success) { success('data:image/jpeg;base64,' + blobInfo.base64()); } }); // 获取内容功能 document.getElementById('submit-btn').addEventListener('click', function () { const content = tinymce.get('myeditor').getContent(); document.getElementById('content-preview').innerHTML = content; console.log('编辑器内容:', content); }); </script> </body> </html>

这个感觉好多了

可以直接黏贴具有格式的文本。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/30 15:56:14

简单理解:DS18B20 驱动的宏定义(部分)

// 宏定义 #define DS18B20_IO_OUT() GPIO_DirectionConfig(DS18B20_PORT, DS18B20_PIN, GPIO_DIR_OUT) #define DS18B20_IO_IN() GPIO_DirectionConfig(DS18B20_PORT, DS18B20_PIN, GPIO_DIR_IN) #define DS18B20_DQ_OUT GPIO_ClearOutBits(DS18B20_PORT, DS18B20…

作者头像 李华
网站建设 2026/6/6 13:14:12

OpenGL ES ->图片纹理不变形显示:两层宽高比校正详解

OpenGL ES 图片纹理不变形显示&#xff1a;两层宽高比校正详解 在 OpenGL ES 中把一张图片正确显示到屏幕上&#xff0c;需要解决两个完全不同的宽高比问题。本文用一个完整的数值示例&#xff0c;从顶点定义到最终像素&#xff0c;讲清楚每一步为什么必须这样做。一、核心矛盾…

作者头像 李华
网站建设 2026/6/13 23:11:23

小红的二叉树【牛客tracker 每日一题】

小红的二叉树 时间限制&#xff1a;1秒 空间限制&#xff1a;1024M 知识点&#xff1a;数论 网页链接 牛客tracker 牛客tracker & 每日一题&#xff0c;完成每日打卡&#xff0c;即可获得牛币。获得相应数量的牛币&#xff0c;能在【牛币兑换中心】&#xff0c;换取相…

作者头像 李华
网站建设 2026/6/4 12:36:19

苹果应用隐私政策配置指南

引言 在开发iOS应用的过程中,隐私政策的配置是一个不可忽视的重要环节。苹果公司对应用的隐私保护有着严格的要求,如果不正确配置隐私信息,可能会导致应用无法通过审核。本文将详细介绍如何配置苹果应用的隐私政策,并通过一个实际案例来展示解决常见问题的步骤。 理解隐私…

作者头像 李华
网站建设 2026/6/6 16:48:37

多线程Web爬虫:如何避免超时错误

在解决LeetCode的多线程Web爬虫问题时,我发现一个有趣的现象:使用ThreadPoolExecutor时,代码可能会超时,即使是在非常简单的测试用例中。今天,我们来探讨一下为什么会发生这种情况,并提供一个优化方案。 问题分析 首先,让我们回顾一下原始的代码实现: class Solutio…

作者头像 李华