LinkXSystem / learn-guide

有趣的学习笔记 (*^_^*)
https://linkxsystem.github.io/
3 stars 1 forks source link

关于富文本编辑器的实现思路 #10

Open LinkXSystem opened 4 years ago

LinkXSystem commented 4 years ago

描述实现一个简单富文本的思路

功能设计:

LinkXSystem commented 4 years ago

实现富文本编辑器的基础

实现富文本编辑器的方式是将 HTML 中的元素变成可编辑模式,而开始编辑模式的方法便是下面的两种:

<div class="editor"></div>
<script>
  const editor = document.querySelector('.editor');
  editor. contenteditable = true
</script>
<iframe src="./editor.html" name="editor" style="width: 640px; height: 480px;"></frame>

<script>
  window.addEventListener('load', function() {
     window.frames['editor'].document.designMode = 'on'
  });
</script>

只有开启了编辑模式,我们才可以在 HTML 中使用 document.exeCommand 方法来动态更新用户希望操作的内容