使用h5的全局属性contenteditable可以让dom元素及其子元素变的可编辑
<div contenteditable id="editor"> </div>
样式代码
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
}* {
margin: 0;
padding: 0;
}
#editor {
width: 100%;
height: 100%;
outline: none;
padding-left: 15px;
}* chrome 49下测试有效
以下方式使得用户初始输入的文本内容在p元素的包裹下
<div contenteditable id="editor" spellcheck="false"><p><br/></p></div>
默认规则如下


否则将直接作为#editor元素的文本节点,即<div contenteditable id="editor" spellcheck="false">文本内容</div>同事点击Enter将新增div元素,即<div contenteditable id="editor" spellcheck="false">文本内容<div></div></div>
#editor中的所用元素都是可被删除的,当#editor为空元素时,用户再次输出内容还会应用默认规则,这里要监听这一状态,发生时将<p><br/></p>添入其中,并且定位光标到p元素的最后
定位光标代码
function cursorToEnd(element){
element.focus();var range = window.getSelection();
range.selectAllChildren(element);
range.collapseToEnd();
}window.getSelection() IE9已经支持
不定位可能发生以下情况
<div contenteditable id="editor" spellcheck="false">
111111
<p><br/></p>
</div>以上就是H5富文本编辑器的详细介绍的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号