首頁  >  文章  >  web前端  >  JS簡易版富文本編輯器實作程式碼

JS簡易版富文本編輯器實作程式碼

小云云
小云云原創
2018-03-26 13:22:263472瀏覽

直到今天才來,初略的了解了下,當然呢,至於過程也是前一秒痛苦,後三秒輕鬆加容易的。這個富文本編輯器,主要是用p自帶的contenteditable屬性document.execCommand()方法實現的,為了方便佈局,偷了下小懶,直接拿table佈局了,唉,作為一名那些年的前端開發人員,還真是不知道該說些啥了。

以下展示實現的效果:


#體的實作過程:

(1) HTML結構:


<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>

(2)JS實作邏輯:


(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();

})();

相關推薦:

簡單實作JavaScript 富文字編輯器的方法

#

以上是JS簡易版富文本編輯器實作程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn