[英]Make wysiwyg editor in html
I will make a wysiwyg 1 editor for html. 我将为HTML制作一个wysiwyg 1编辑器。 I've made a text area, but how can I make a wysiwyg editor for it?
我已经创建了文本区域,但是如何为其创建所见即所得的编辑器呢? I would be easy if the text area implement html tags like snippet below, but it doesn't.
如果文本区域实现以下snippet之类的html标签,我会很容易,但是事实并非如此。 How can I make this wysiwyg editor?
如何制作该所见即所得的编辑器?
<textarea cols="30" rows="10"> <h1>Title</h1> <hr/> <p>Some text</p> <ul> <li>a</li> <li>b</li> <li>c</li> </ul> </textarea>
1 w hat y ou s ee i s w hat y ou g et 1瓦特帽子Ÿ欧小号 EE I S 瓦特帽子Ÿ欧G等
WYSIWYG editors are not built into web browsers. 所见即所得编辑器未内置在Web浏览器中。 You'll have to use a library or write a bit of code yourself.
您必须自己使用一个库或编写一些代码。
You can do a quick search on GitHub to find WYSIWYG editors for JavaScript. 您可以在GitHub上进行快速搜索,以找到JavaScript的WYSIWYG编辑器。
I haven't used any of these but these seem like places to start. 我没有使用任何这些,但是这些似乎是开始的地方。
You can use following code... 您可以使用以下代码...
window.addEventListener("load", function(){ //first check if execCommand and contentEditable attribute is supported or not. if(document.contentEditable != undefined && document.execCommand != undefined) { alert("HTML5 Document Editing API Is Not Supported"); } else { document.execCommand('styleWithCSS', false, true); } }, false); //underlines the selected text function underline() { document.execCommand("underline", false, null); } //makes the selected text as hyperlink. function link() { var url = prompt("Enter the URL"); document.execCommand("createLink", false, url); } //displays HTML of the output function displayhtml() { //set textContent of pre tag to the innerHTML of editable div. textContent can take any form of text and display it as it is without browser interpreting it. It also preserves white space and new line characters. document.getElementsByClassName("htmloutput")[0].textContent = document.getElementsByClassName("editor")[0].innerHTML; }
.editor { width: 500px; height: 500px; background-color: yellow; }
<button onclick="underline()">Underline Text</button> <button onclick="link()">Link</button> <button onclick="displayhtml()">Display HTML</button> <!-- Make it content editable attribute true so that we can edit inside the div tag and also enable execCommand to edit content inside it.--> <div class="editor" contenteditable="true" spellcheck="false"> This is the text editor </div> <div class="codeoutput"> <!-- <pre> tags reserves whitespace and newline characters. --> <pre class="htmloutput"> </pre> </div>
Reference: labs.qnimate.com/wysiwyg-editor 参考: labs.qnimate.com/wysiwyg-editor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.