Home > Web Front-end > H5 Tutorial > body text

Detailed introduction of H5 rich text editor

零下一度
Release: 2017-07-21 14:10:04
Original
3449 people have browsed it

Use the H5 global attribute contenteditable to make DOM elements and their sub-elements editable

<div  contenteditable id="editor">
   </div>
Copy after login

Style code

html,
body {
  overflow: hidden;
  width: 100%;
  height: 100%;
}* {
  margin: 0;
  padding: 0;
}
#editor {
  width: 100%;
  height: 100%;
  outline: none;
  padding-left: 15px;
}
Copy after login

* Tested under Chrome 49 and valid

The following method makes the text content initially entered by the user be wrapped in the p element

<div  contenteditable id="editor" spellcheck="false"><p><br/></p></div>
Copy after login

The default rules are as follows

否则将直接作为#editor元素的文本节点,即<div  contenteditable id="editor" spellcheck="false">文本内容</div>同事点击Enter将新增div元素,即<div  contenteditable id="editor" spellcheck="false">文本内容<div></div></div>
Copy after login
View Code

#All elements used in editor are It can be deleted. When #editor is an empty element, the default rules will be applied when the user outputs content again. This state needs to be monitored here. When it occurs,


Enter it, and position the cursor to the end of the p element

Positioning cursor code

function cursorToEnd(element){
    
    element.focus();var range = window.getSelection();

    range.selectAllChildren(element);
    range.collapseToEnd();
    
}
Copy after login

window.getSelection() IE9 already supports

The following situations may occur if positioning is not performed

<div  contenteditable id="editor" spellcheck="false">
    111111
    <p><br/></p>
</div>
Copy after login

The above is the detailed content of Detailed introduction of H5 rich text editor. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template