首頁 > web前端 > H5教程 > 主體

H5富文本編輯器的詳細介紹

零下一度
發布: 2017-07-21 14:10:04
原創
3397 人瀏覽過

使用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元素的最後

定位遊標代碼

#
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中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!