PC端使用wangeditor富文本(JS引用方式)

本文档详细介绍了如何在网页中集成WangEditor富文本编辑器,并配置图片和视频上传功能。通过设置上传参数、自定义上传函数,实现了将图片和视频上传到本地服务器,并在编辑器中插入。同时,还展示了获取和设置富文本内容,以及禁用编辑器的方法,适合前端开发者参考学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JS使用wangeditor记录

官网富文本样式

wangeditor官网
wangeditor官方文档

首先引入所用JS,也可下载到本地使用

(综合多方因素…我是下载到本地使用的)

1、创建div标签

<div id="richText"></div>

2、引入js

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>

3、基本操作(配置图片、视频上传到本地服务器)

<script type="text/javascript">
	// 获取本地存储的token
	var token = localStorage.getItem('token');
	const E = window.wangEditor
	editor = new E("#richText")
	// 忽略复制粘贴的图片
	editor.config.pasteIgnoreImg = true
	// 隐藏插入网络图片功能
	editor.config.showLinkImg = false
	// 自定义上传参数
	editor.config.uploadImgParams = {
		token: token,
	}
	// 自定义header
	editor.config.uploadImgHeaders = {
		Authorization: token,
	}
	
	// 配置图片上传地址(自定义上传图片同级,二者有一即可)
	// editor.config.uploadImgServer = apiUrlFn("/jlmanager/upload/uploadFile")
	
	// 自己实现上传图片
	editor.config.customUploadImg = function (resultFiles, insertImgFn) {
		// resultFiles 是 input 中选中的文件列表
		// insertImgFn 是获取图片 url 后,插入到编辑器的方法
		var files = resultFiles[0]
		var formData = new FormData();
		formData.append("file", files); //加入文件对象
		formData.append('multipartFile', files);
		$.ajax({
			url: '上传图片接口',
			type: 'POST',
			data: formData,
			cache: false,
			contentType: false,
			processData: false,
			dataType: "json",
			success: function(res) {
				if (res.code == 200) {
					// 上传图片,返回结果,将图片插入到编辑器中
					insertImgFn('上传图片后返回的url路径')
					console.log('上传成功!');
				} else {
					console.log('上传失败!');
				}
			}
		})
	}
	
	// 隐藏插入网络视频的功能
	editor.config.showLinkVideo = false
	// 自定义上传参数
	editor.config.uploadVideoParams = {
		token: token,
	}
	// 自定义 header
	editor.config.uploadVideoHeaders = {
		Authorization: token,
	}
	// 自定义上传视频
	editor.config.customUploadVideo = function (resultFiles, insertVideoFn) {
		// resultFiles 是 input 中选中的文件列表
		// insertVideoFn 是获取视频 url 后,插入到编辑器的方法
		var files = resultFiles[0]
		var formData = new FormData();
		formData.append("file", files); //加入文件对象
		formData.append('multipartFile', files);
		$.ajax({
			url: '上传视频接口',
			type: 'POST',
			data: formData,
			cache: false,
			contentType: false,
			processData: false,
			dataType: "json",
			success: function(res) {
				console.log(res)
				if (res.code == 200) {
					// 上传视频,返回结果,将视频地址插入到编辑器中
					insertVideoFn('上传视频后返回的url路径)
					console.log('上传成功!');
				} else {
					console.log('上传失败!');
				}
			}
		})
	}
	
	// 最后执行create()
	editor.create()
</script>

4、获取富文本内容

editor.txt.html()

5、设置富文本内容

editor.txt.html('<h3 style="text-align:center;"><a><b>设置内容试试</b></a></h3>')

禁用富文本

editor.disable()

菜鸟本菜描述

毕竟是菜鸟本菜,第一次使用这个富文本,及时记录,方便学习

wangEditor是一款基于JavaScript和CSS开发的Web富文本编辑器,它具有轻量级、简洁、易用的特点。相比于普通的文本编辑器,富文本编辑器可以输入超越文本的数据内容,包括上传图片、输入表情、字体大小字号调整、颜色设置、对齐方式等功能操作。\[1\] 在使用wangEditor富文本编辑器时,首先需要引入相关代码。在editor.vue文件中,可以使用以下代码引入富文本编辑器: ```html <template> <div> <div ref="editor" style="text-align:left"></div> </div> </template> <script> import E from 'wangeditor' export default { name: 'MyEditor', data() { return { editorContent: '', editor: null } }, props: { value: { type: String, required: true } }, model: { prop: 'value' }, methods: { getContent: function () { alert(this.editorContent) }, _initEditor(that) { var editor = new E(this.$refs.editor) editor.config.zIndex = 100 editor.create() that.editor = editor } }, mounted() { this._initEditor(this) setTimeout(() => { this.editor.txt.html(this.value) }, 1000) } } </script> <style scoped> </style> ``` 以上代码是一个使用wangEditor的基本示例,通过在组件中引入wangEditor并进行相关配置,可以实现富文本编辑功能。\[2\] 如果在项目中多个地方都需要使用富文本编辑器,可以将富文本编辑器封装成一个组件,以减少重复代码。具体的使用方法可以参考相关文档和示例。\[3\] #### 引用[.reference_title] - *1* [富文本编辑器wangEditor使用(即学即用)](https://blog.csdn.net/kid00712138/article/details/122495640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [wangEditor富文本编辑器(第一章:基础使用)](https://blog.csdn.net/DW14687/article/details/118440176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值