JS simple version rich text editor implementation code
I didn’t come here until today, and I have a rough understanding of it. Of course, as for the process, it is painful in the first second and easy in the last three seconds. This rich text editor is mainly implemented using the contenteditable attribute document.execCommand() method that comes with p. In order to facilitate the layout, I was lazy and directly used the table layout. Alas, as a front-end developer in those years Staff, I really don’t know what to say.
The following shows the effect of the implementation:
The implementation process of the body:
(1) HTML structure:
<table border='1' class="tablebox" id='tablebox'> <tr> <td> <input type="button" name="bold" value='Bold' class="bold"> </td> <td> <input type="button" name="italic" value='Italic' class="italic"> </td> <td> <input type="button" name="underline" value='Underline' class="decotation"> </td> <td>size <select name="fontSize" class="font"> <option value="1">1</option> <option value="3">3</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> </select> </td> <td>img <select name="insertImage"> <option value="">请选择图片</option> <option value="timg.jpg">timg.jpg</option> <option value="timg1.jpg">timg1.jpg</option> <option value="timg2.jpg">timg2.jpg</option> <option value="timg3.jpg">timg3.jpg</option> <option value="timg4.jpg">timg4.jpg</option> </select> </td> <td> <input type="button" name="selectAll" value='全选' class="selectAll"> </td> <td> <input type="button" name="undo" value='撤销' class="undo"> </td> <td> <input type="button" name="justifyLeft" value='left' class="justifyLeft"> </td> <td> <input type="button" name="justifyCenter" value='center' class="justifyCenter"> </td> <td> <input type="button" name="justifyRight" value='right' class="justifyRight"> </td> </tr> <tr> <td colspan='10'> <p class="text" contenteditable="true">这是一个用p的contenteditable属性以及document.execCommand实现的一个简易富文本编辑器。</p> </td> </tr> </table>
(2) JS implementation logic:
(function() { //富文本编辑器类 class Editor { constructor() { this.bindElem(); } bindElem() { var text = document.querySelector('.text'); var txt = null; var tablebox = document.getElementById_x('tablebox'); var inputbs = tablebox.querySelectorAll('input,select'); for (var i = 0; i { if (inputbs[i].tagName.toLowerCase() == 'input') { this.action(inputbs[i], inputbs[i].name); } else if (inputbs[i].tagName.toLowerCase() == 'select') { inputbs[i].onchange = function() { document.execCommand(this.name, true, this.value); } } } } action(obj, attr) { obj.onclick = function() { document.execCommand(attr, true); } } } new Editor(); })();
Related recommendations:
A simple way to implement a JavaScript rich text editor
The above is the detailed content of JS simple version rich text editor implementation code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



C language is a basic and important programming language. For beginners, it is very important to choose appropriate programming software. There are many different C programming software options on the market, but for beginners, it can be a bit confusing to choose which one is right for you. This article will recommend five C language programming software to beginners to help them get started quickly and improve their programming skills. Dev-C++Dev-C++ is a free and open source integrated development environment (IDE), especially suitable for beginners. It is simple and easy to use, integrating editor,

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

Title: Introduction to Go language development tools: List of essential tools In the development process of Go language, using appropriate development tools can improve development efficiency and code quality. This article will introduce several essential tools commonly used in Go language development, and attach specific code examples to allow readers to understand their usage and functions more intuitively. 1.VisualStudioCodeVisualStudioCode is a lightweight and powerful cross-platform development tool with rich plug-ins and functions.

With the popularity and popularity of Golang, more and more developers are beginning to use this programming language. However, like other popular programming languages, Golang development requires choosing a suitable editor to improve development efficiency. In this article, we will introduce five editors suitable for Golang development. VisualStudioCodeVisualStudioCode (VSCode for short) is a free cross-platform editor developed by Microsoft. It is based on Elect

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Detailed explanation of VSCode functions: How does it help you improve work efficiency? With the continuous development of the software development industry, developers' pursuit of work efficiency and code quality have become important goals in their work. In this process, the choice of code editor becomes a key decision. Among many editors, Visual Studio Code (VSCode for short) is loved by the majority of developers for its powerful functions and flexible scalability. This article will introduce some functions of VSCode in detail and discuss

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
