Home Web Front-end JS Tutorial JS simple version rich text editor implementation code

JS simple version rich text editor implementation code

Mar 26, 2018 pm 01:22 PM
javascript text editor

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=&#39;1&#39; class="tablebox" id=&#39;tablebox&#39;>
    <tr>
        <td>
            <input type="button" name="bold" value=&#39;Bold&#39; class="bold">
        </td>
        <td>
            <input type="button" name="italic" value=&#39;Italic&#39; class="italic">
        </td>
        <td>
            <input type="button" name="underline" value=&#39;Underline&#39; 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=&#39;全选&#39; class="selectAll">
        </td>
        <td>
            <input type="button" name="undo" value=&#39;撤销&#39; class="undo">
        </td>
        <td>
            <input type="button" name="justifyLeft" value=&#39;left&#39; class="justifyLeft">
        </td>
        <td>
            <input type="button" name="justifyCenter" value=&#39;center&#39; class="justifyCenter">
        </td>
        <td>
            <input type="button" name="justifyRight" value=&#39;right&#39; class="justifyRight">
        </td>
    </tr>
    <tr>
        <td colspan=&#39;10&#39;>
            <p class="text" contenteditable="true">这是一个用p的contenteditable属性以及document.execCommand实现的一个简易富文本编辑器。</p>
        </td>
    </tr>
</table>
Copy after login

(2) JS implementation logic:


(function() {
	//富文本编辑器类
	class Editor {

		constructor() {
			this.bindElem();
		}


		bindElem() {
			var text = document.querySelector(&#39;.text&#39;);
			var txt = null;
			var tablebox = document.getElementById_x(&#39;tablebox&#39;);
			var inputbs = tablebox.querySelectorAll(&#39;input,select&#39;);

			for (var i = 0; i {
					if (inputbs[i].tagName.toLowerCase() == &#39;input&#39;) {
						this.action(inputbs[i], inputbs[i].name);
					} else if (inputbs[i].tagName.toLowerCase() == &#39;select&#39;) {
						inputbs[i].onchange = function() {
							document.execCommand(this.name, true, this.value);
						}
					}
				}
			}


			action(obj, attr) {
				obj.onclick = function() {
					document.execCommand(attr, true);
				}
			}

		}

	new Editor();

})();
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Essential software for C language programming: five good helpers recommended for beginners Essential software for C language programming: five good helpers recommended for beginners Feb 20, 2024 pm 08:18 PM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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

How to search for text across all tabs in Chrome and Edge How to search for text across all tabs in Chrome and Edge Feb 19, 2024 am 11:30 AM

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

Introduction to Go language development tools: a list of essential tools Introduction to Go language development tools: a list of essential tools Mar 29, 2024 pm 01:06 PM

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.

Golang editor recommendations: five choices suitable for development Golang editor recommendations: five choices suitable for development Jan 19, 2024 am 09:00 AM

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 forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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? Detailed explanation of VSCode functions: How does it help you improve work efficiency? Mar 25, 2024 pm 05:27 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

See all articles