用纯js实现一个文本编辑器
效果图

代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.editButtons span{
display:inline-block;
white-space:nowrap;
}
[data-edit] {
float:left;
border:0;
font: 12px/1 monospace;
background:#ddd;
padding:4px 8px;
}
[contenteditable] {
padding:4px 16px;
background:#eee;
}
</style>
</head>
<body>
<div class="editButtons">
<span title="STYLES">
<!-- 加粗 -->
<button data-edit="bold"><b>B</b></button>
<!-- 斜体 -->
<button data-edit="italic"><i>I</i></button>
<!-- 下划线 -->
<button data-edit="underline"><u>U</u></button>
<!-- 中划线 -->
<button data-edit="strikeThrough"><s>S</s></button>
</span>
<span title="TEXT FORMAT">
<!-- 字体标签设置 -->
<button data-edit="formatBlock:p">P</button>
<button data-edit="formatBlock:H1">H1</button>
<button data-edit="formatBlock:H2">H2</button>
<button data-edit="formatBlock:H3">H3</button>
</span>
<span title="FONT SIZE">
<!-- 字体大小设置 -->
<button data-edit="fontSize:1">s</button>
<button data-edit="fontSize:3">M</button>
<button data-edit="fontSize:5">L</button>
</span>
<span title="LISTS">
<!-- 列表格式设置 -->
<button data-edit="insertUnorderedList">UL</button>
<button data-edit="insertOrderedList">OL</button>
</span>
<span title="ALIGNMENT">
<!-- 文本对齐设置 -->
<button data-edit="justifyLeft">⇤</button>
<button data-edit="justifyCenter">↔</button>
<button data-edit="justifyRight">⇥</button>
</span>
<span title="CLEAR FORMATTING">
<!-- 清除文本 -->
<button data-edit="removeFormat">×</button>
</span>
<span title="COPY">
<!-- 复制选中文本 -->
<button data-edit="copy">C</button>
</span>
</div>
<div contenteditable id="textbox">
<p>文本编辑器</p>
</div>
</body>
<script>
document.querySelectorAll("[data-edit]").forEach(btn =>
btn.addEventListener("click", function (ev) {
ev.preventDefault();
console.log(this.getAttribute("data-edit"))
const cmd_val = this.getAttribute("data-edit").split(":");
if(cmd_val[0] == 'copy'){ // 复制选中的文本
document.execCommand(cmd_val[0]);
}else{
document.execCommand(cmd_val[0], false, cmd_val[1]);
}
})
);
</script>
</html>
用纯js实现一个文本编辑器的更多相关文章
- js 判断一个文本框是否获得焦点
1.js 判断一个文本框是否获得焦点 // 可以用document.activeElement判断 // document.activeElement表示当前活动的元素 // 查找你要判断的文本框 ...
- 如何使用纯js实现一个带有灰色半透明背景的弹出框
原文如何使用纯js实现一个带有灰色半透明背景的弹出框 // 加入透明背景 var body = document.body;var backgroundDiv = document.createEle ...
- 手把手教你使用 js 实现一个 Canvas 编辑器
手把手教你使用 js 实现一个 Canvas 编辑器 拖拽 缩放,等比缩放 导出 image 模版 撤销,重做 OOP,封装,继承,多态 发布库 CI/CD (gitlab/github) ... h ...
- draft.js开发富文本编辑器
写在前头的话 在react中去寻找一个好用的富文本编辑器网上很少有推荐的,搜到的也只有一些个人不成熟的作品,慢慢发现网上比较推荐的一个东东叫做draft.js. 这个东西在网上可以找到的教程也是手指头 ...
- js中的文本编辑器控件KindEditor---那些打酱油的日子
使用文本编辑器控件KindEditor渲染文本域页面显示 this.sync()同步KindEditor的值到textarea文本框 editor.isEmpty()判断文本域是否是空 editer. ...
- js处理富文本编辑器转义、去除转义、去除HTML标签
富文本编辑器生成的HTML标签,进行转义,然后写入数据库,防止脚本注入: function htmlEncode(value){ return $('<div/>').text(value ...
- js中的文本编辑器控件KindEditor
使用文本编辑器控件KindEditor渲染文本域页面显示 this.sync()同步KindEditor的值到textarea文本框 editor.isEmpty()判断文本域是否是空 editer. ...
- Vue.js示例:文本编辑器。使用_.debounce()反抖动函数
Markdown编辑器 https://cn.vuejs.org/v2/examples/index.html 新知识: Underscore.js库 用于弥补标准库,方便了JavaScript的编程 ...
- Qt学习笔记-制作一个文本编辑器
创建一个MainWindow工程.添加一个TextEdit.垂直布局. 在menu上面创建新的action. 在新建的时候判断文本有没有被修改. 下面,将某个文件打开读入到TextEdit中. 保存文 ...
随机推荐
- C++(十九) — const 和 #define 区别
1.const (1)C++对 const 常量的处理过程:当编译器碰到 常量声明 时,在符号表中放入常量,编译时发现使用常量,则直接以符号表中的值替换. (2)如果,编译中发现,对 const 使 ...
- cenOs7安装redis
1.安装redis redis 目前没有官方 RPM 安装包,我们需要从源代码编译,而为了要编译就需要安装 Make 和 GCC. 如果没有安装过 GCC 和 Make,那么就使用 yum 安装 -- ...
- discuz! 设置私密论坛版块的方法
Discuz!的强大功能不用细说, 话说对于有一部分需要设置具有一定访问权限的用户才能浏览的版块内容的话. 可能很多朋友不太清楚, 为了解决这个问题, 第一步以管理员的身份登陆, 然后 论坛-> ...
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- bulkcopy实现批量插入与更新
public static void UpdateData<T>(List<T> list, string TabelName) { DataTable dt = new Da ...
- 【解题报告】[动态规划] - PID90 / 未出现的子串
原题地址:http://www.rqnoj.cn/problem/90 解题思路:题目看起来不太像动态规划... 我用一个数组f[i][j]来表示在数组第i个元素的后面第一次出现j的位置,为-1则是没 ...
- 第九集 经验风险最小化(ERM)
实在写不动了,将word文档转换为PDF直接截图了... 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 安装PL/SQL客户端来访问操作步骤
1 2 3.改变路径 4 5 6 7 8 9.选择注册机 10
- Python学习-括号
python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也叫做花括号{ }.其作用也各不相同,分别用来代表不同的python基本内置数据类型. 1.python中的小括号( ): ...
- vs2015 c++ _findnext 报错
定位 _findnext(hFile,&fileinfo) 报错. 错误 :0x00007FFC70CB0B2D (ntdll.dll)处(位于 Cutton_Dlg.exe 中)引发的异常: ...