html富文本编辑器

news2024/9/22 19:25:03

接了个单子,需要添加一个文章模块,一看用到的技术这么老,人傻了,纯html css js 。

在普通页面中

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100;
        }

        #toolbar-container {
            border-bottom: 1px solid #ccc;
        }

        #editor-container {
            height: 500px;
        }

        .row {
            width: 98%;
            padding-left: 10px;
            padding-right: 10px;
            display: flex;
            justify-content: space-between;
        }

        .half-width {
            width: 45%;
        }

        .marginLeft {
            margin-left: 30px;
        }
    </style>
</head>

<body>

<div class="row marginLeft">
    <div id="editor—wrapper" class="half-width">
        <div id="toolbar-container"><!-- 工具栏 --></div>
        <div id="editor-container"><!-- 编辑器 --></div>
    </div>

    <div class="half-width">
        <h1 style="text-align: center; font-family: '楷体'; margin: 0 0; height: 10%;">展示效果</h1>
        <div id="editor-showNode"  style="border: 1px solid #ccc;height: 90%;"><!-- 编辑器 --></div>
    </div>
</div>

<div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
    <p style="margin-right: 10px; display: inline-block; margin-top: 13px;">文件名称:</p>
    <input style="display: inline-block; width: 80%;" type="text" class="form-control" placeholder="文件名称" aria-describedby="basic-addon2">
</div>

<div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
    <p style="margin-right: 10px; display: inline-block; margin-top: 13px;">添加备注:</p>
    <input style="display: inline-block; width: 80%;" type="text" class="form-control" placeholder="添加备注" aria-describedby="basic-addon2">
</div>
<br/>
<div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
    <p style="margin-right: 10px; display: inline-block; margin-top: 15px;">选择附件:</p>
    <input type="file" class="btn btn-primary" data-toggle="button" aria-pressed="false">
</div>

<div class="d-flex justify-content-center mb-3" style="margin-top: 80px; margin-left: -50px; margin-bottom: 40px">
    <button id="clear-editor" class="btn btn-primary" style="width: 300px;">提交公告摘要</button> <!-- 使用了Bootstrap的按钮样式 -->
</div>
<div style="height: 50px">

</div>

</body>

<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script>
    const { createEditor, createToolbar } = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('', html)  //html是当前编辑器的内容
            // 也可以同步到 <textarea>
            document.getElementById("editor-showNode").innerHTML=html;
        }
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',  //这是编辑器默认显示的内容
        config: editorConfig,
        mode: 'default', // or 'simple'
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    });

    // 为按钮添加点击事件处理函数
    document.getElementById('clear-editor').addEventListener('click', function() {
        // 使用编辑器的 clear 方法来清空内容
        editor.clear()
    })
</script>

</html>

效果

在表单页面中,需要与要上传的文件一起添加到服务器

添加了一个隐藏的<textarea>标签来存储编辑器的内容,在提交表单时更新这个字段的值。改改了了按钮的点击事件处理函数,使其在提交表单时阻止默认的表单提交行为,并通过XMLHttpRequest发送一个POST请求,把所有内容提交到后台。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        #editor—wrapper {
            border: 1px solid #ccc;
            z-index: 100;
        }

        #toolbar-container {
            border-bottom: 1px solid #ccc;
        }

        #editor-container {
            height: 500px;
        }

        .row {
            width: 98%;
            padding-left: 10px;
            padding-right: 10px;
            display: flex;
            justify-content: space-between;
        }

        .half-width {
            width: 45%;
        }

        .marginLeft {
            margin-left: 30px;
        }
    </style>
</head>
<body>
<form id="myForm" method="post" enctype="multipart/form-data">
    <div class="row marginLeft">
        <div id="editor—wrapper" class="half-width">
            <div id="toolbar-container"><!-- 工具栏 --></div>
            <div id="editor-container"><!-- 编辑器 --></div>
        </div>
    
        <div class="half-width">
            <h1 style="text-align: center; font-family: '楷体'; margin: 0 0; height: 10%;">展示效果</h1>
            <div id="editor-showNode"  style="border: 1px solid #ccc;height: 90%;"><!-- 编辑器 --></div>
        </div>
    </div>

    <div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
        <p style="margin-right: 10px; display: inline-block; margin-top: 13px;">文件名称:</p>
        <input name="fileName" style="display: inline-block; width: 80%;" type="text" class="form-control" placeholder="文件名称" aria-describedby="basic-addon2">
    </div>

    <div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
        <p style="margin-right: 10px; display: inline-block; margin-top: 13px;">添加备注:</p>
        <input name="note" style="display: inline-block; width: 80%;" type="text" class="form-control" placeholder="添加备注" aria-describedby="basic-addon2">
    </div>

    <div class="marginLeft" style="margin-top: 50px; display: flex; align-items: center;">
        <p style="margin-right: 10px; display: inline-block; margin-top: 15px;">选择附件:</p>
        <input name="file" type="file" class="btn btn-primary" data-toggle="button" aria-pressed="false">
    </div>

    <textarea id="editorContent" name="editorContent" style="display: none;"></textarea>

    <div class="d-flex justify-content-center mb-3" style="margin-top: 80px; margin-left: -50px; margin-bottom: 40px">
        <button id="submit" class="btn btn-primary" style="width: 300px;">提交公告摘要</button>
    </div>
</form>

<!-- Bootstrap JS -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>

<script>
    const { createEditor, createToolbar } = window.wangEditor

    const editorConfig = {
        placeholder: 'Type here...',
        onChange(editor) {
            const html = editor.getHtml()
            console.log('', html)
            document.getElementById("editor-showNode").innerHTML=html;
        }
    }

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',  
        config: editorConfig,
        mode: 'default', 
    })

    const toolbarConfig = {}

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default',
    });

    document.getElementById('submit').addEventListener('click', function(event) {
        event.preventDefault();  

        document.getElementById('editorContent').value = editor.getHtml();

        var formElement = document.querySelector("form");
        var formData = new FormData(formElement);

        var request = new XMLHttpRequest();  
        request.open("POST", "/submit"); 
        request.send(formData);  
    })
</script>
</body>
</html>

效果

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/814997.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

word xls有用小技巧

不少office、代码编辑等软件&#xff0c;很简单高效小技巧。Word xlsx 某一行或列不动&#xff1a; 视图》冻结窗格》冻结首行 eclispe 全局搜索 CtrlH 制定变量、名称搜索 鼠标左键点中CtrlAltG

Promethus(普罗米修斯)监控系统

一、普罗米修斯概述 Prometheus(由go语言(golang)开发)是一套开源的监控&报警&时间序列数据库的组合。适合监控docker容器。因为kubernetes(俗称k8s)的流行带动了prometheus的发展。 Overview | Prometheus 二、时间序列数据 1、什么是序列数据 时间序列数据(TimeSer…

Docker安装es以及ik分词器

1、拉取镜像 docker pull elasticsearch:7.10.12、下载对应版本的ik分词、并将它们解压到ik文件夹下&#xff0c;如图 https://github.com/medcl/elasticsearch-analysis-ik/releases 3、在服务器上创建文件夹 mkdir /usr/elklog/elk/es mkdir /usr/elklog/elk/es/data mkdi…

Android 架构模式如何选择

作者&#xff1a;vivo 互联网客户端团队-Xu Jie Android架构模式飞速演进&#xff0c;目前已经有MVC、MVP、MVVM、MVI。到底哪一个才是自己业务场景最需要的&#xff0c;不深入理解的话是无法进行选择的。这篇文章就针对这些架构模式逐一解读。重点会介绍Compose为什么要结合MV…

脑电信号处理与特征提取——6.运用机器学习技术和脑电进行大脑解码(涂毅恒)

目录 六、运用机器学习技术和脑电进行大脑解码 6.1 前言 6.2 基于脑电数据的机器学习基础分析 6.3 基于脑电数据的机器学习进阶分析 6.4 代码解读 六、运用机器学习技术和脑电进行大脑解码 6.1 前言 6.2 基于脑电数据的机器学习基础分析 6.3 基于脑电数据的机器学习进阶分…

反射简述

什么是反射反射在java中起到什么样的作用获取class对象的三种方式反射的优缺点图 什么是反射 JAVA反射机制是在运行状态中&#xff0c;对于任意一个类&#xff0c;都能够知道这个类的所有属性和方法&#xff1b;对于任意一个对象&#xff0c;都能够调用它的任意一个方法和属性&…

【yolov8+人/车流量统计】

文章目录 前言修改点PreprocessInference 总结 前言 之前写个yolov8的一个试用版&#xff0c;【深度学习】Yolov8追踪从0到1, 这要是做计数啥的,简单的一批&#xff0c;一套工程化的代码&#xff0c;给自己挖了个坑&#xff0c;说要实现一个基于yolov8的人/车流量统计. 现在要…

如何学习ARM嵌入式开发?

ARM和单片机还是有许多区别的&#xff0c;可以说比单片机的应用更为复杂吧&#xff0c;往往在单片机里只需要对一个寄存器赋值就可以的初始化&#xff0c;在ARM下就要调用库函数了。甚至每个引脚其功能都多了许多&#xff0c;相应的配置也会更为麻烦&#xff0c;但如果做多了AR…

回归分析简明教程【Regression Analysis】

为了理解回归背后的动机&#xff0c;让我们考虑以下简单的例子。 下面的散点图显示了2001年至2012年美国大学毕业生的数量。 现在根据现有的数据&#xff0c;如果有人问你2018年有多少名大学毕业生获得硕士学位呢&#xff1f; 可以看出&#xff0c;具有硕士学位的大学毕业生数量…

【数据挖掘torch】 基于LSTM电力系统负荷预测分析(Python代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

Mitmproxy的使用

目录 1.背景 2.Mitmproxy 1&#xff09;Windows安装 2&#xff09;定制开发 3&#xff09;启动 3.使用 4.问题 5.参考资料 1.背景 简单来说&#xff0c;背景就两个字“偷懒”&#xff0c;一个字“懒”。在API自动化脚本的开发中&#xff0c;一般都需要封装一下接口。如果没…

2023届浙江大学MPA提面A资格经验总结分享

本人是去年报考的浙大MPA项目&#xff0c;并通过提面获得了A资格&#xff0c;新一年浙大MPA项目提前批面试已经开始了&#xff0c;受达立易考周老师邀请来分享下我的提面经验&#xff0c;希望我的经验能对还在迷茫中的小伙伴有所帮助。 点开提面通知&#xff0c;首先看到…

2023项目管理产品排行榜:优化企业项目管理的顶级选择

随着全球竞争加剧和商业环境的变化&#xff0c;企业对项目管理的需求越来越迫切。优秀的项目管理产品能够帮助企业提高工作效率、资源利用率和项目交付质量。 本文参考了不同的产品测评网站&#xff0c;在众多项目管理产品中&#xff0c;总结了以下几款备受好评的项目管理工具&…

UNISOT让食品欺诈无处遁形

​​发表时间&#xff1a;2023年5月11日 全世界的消费者开始越来越关注食物的来源和采购方式。这是因为人们渴望吃得更健康、更用心&#xff0c;同时人们也认识到了购买可持续且合乎伦理道德的产品的必要性。 近年来&#xff0c;人们对食品溯源的渴望进一步加速&#xff0c;原…

go 中的代码漏洞检查

前言 不知道大家在开发 go 项目中有没有遇到过一些第三方包或者官方包中出现漏洞的问题&#xff0c;这些漏洞可能会影响到代码的功能、性能或者安全性。 现在针对这一问题&#xff0c;go 团队提供了 govulncheck 工具&#xff0c;帮助开发者快速地发现和修复这些漏洞。 什么…

一套AI+医疗模式的医院智慧导诊系统源码:springboot+redis+mybatis plus+mysql

一套AI医疗模式的医院智慧导诊系统源码 相关技术&#xff1a; 技术架构&#xff1a;springbootredismybatis plusmysqlRocketMQ 开发语言&#xff1a;java 开发工具&#xff1a;IDEA 前端框架&#xff1a;Uniapp 后端框架&#xff1a;springboot 数 据 库&#xff1a;mys…

利用Windows自带组件安全清理WinSXS目录的几种方法

WinSXS&#xff08;Windows Side-by-side&#xff09;是 Windows 操作系统的核心组件之一&#xff0c;位于系统目录下的一个文件夹中&#xff0c;它包含了操作系统的许多重要文件和组件&#xff0c;以及更新时对老旧软件版本的备份&#xff0c;它会随着时间的推移不断增加&…

ssh安全远程管理

目录 1、什么是ssh 2、ssh登陆 3、ssh文件传输 1、什么是ssh ssh是 Secure Shell 的缩写&#xff0c;是一个建立在应用层上的安全远程管理协议。ssh 是目前较为可靠的传输协议&#xff0c;专为远程登录会话和其他网络服务提供安全性。利用ssh 协议可以有效防止远程管理过程中…

vue3 axios接口封装

在Vue 3中&#xff0c;可以通过封装axios来实现接口的统一管理和调用。封装后的接口调用更加简洁&#xff0c;代码可维护性也更好。以下是一个简单的Vue 3中axios接口封装的示例&#xff1a; 1.首先&#xff0c;安装axios和qs&#xff08;如果需要处理复杂数据&#xff09;&am…

C#winform颜色、字体对话框控件使用实例

本文讲解C#winform颜色、字体对话框控件的使用 创建winform项目,添加按钮 代码如下: using System; using System.Drawing; using System.Windows.Forms;namespace ColorDialogDemo {public partial class Form1 : Form{public Form1(){InitializeComponent();}//颜色对话框…