发布网友 发布时间:2022-04-21 07:28
共2个回答
懂视网 时间:2022-05-12 06:26
使用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>View Code
#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>
热心网友 时间:2022-05-12 03:34
富文本编辑器,Rich Text Editor, 简称 RTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器。它提供类似于 比如谷歌、火狐效果都非常不错,在功能的丰富性来说,还是 IE 强些(但IE的6、7、8、9几个版本就足够搞死开发者了)。富文本编辑器不同于文本编辑器,程序员可到网上下载免费的富文本编辑器内嵌于自己的网站或程序里(当然付费的功能会更强大些),方便用户编辑文章或信息。比较好的文本编辑器有kindeditor,fckeditor等。