基於jquery的bootstrap在線文本編輯器插件Summernote (轉)


bootstrap在線文本編輯器插件Summernote

Summernote是一個基於jquery的bootstrap超級簡單WYSIWYG在線編輯器。Summernote非常的輕量級,大小只有30KB,支持Safari,Chrome,Firefox、Opera、Internet Explorer 9 +(IE8支持即將到來)。

特點:

世界上最好的WYSIWYG在線編輯器

極易安裝

開源

自定義初化選項

支持快捷鍵

適用於各種后端程序言語

使用方法

使用HTML5文檔

1
2
3
4
<!DOCTYPE html>
< html >
...
</ html >

引入核心文件,Summernote需要幾個JS庫的支持,所以得先引入其它庫

1
2
3
4
5
6
7
8
9
<!-- include libries(jQuery, bootstrap, fontawesome) -->
< script  src = "//code.jquery.com/jquery-1.9.1.min.js" ></ script >
< link  href = "//netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css" >
< script  src = "//netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.min.js" ></ script >
< link  href = "//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" >
 
<!-- include summernote css/js-->
< link  href = "summernote.css"  />
< script  src = "summernote.min.js" ></ script >

寫入html,只需加入一個DIV元素,寫上ID

1
< div  id = "summernote" >Hello Summernote</ div >

寫入JS初始化插件

1
2
3
$(document).ready( function () {
         $( '#summernote' ).summernote();
     });

API

初始化Summernote

1
$( '.summernote' ).summernote();

使用參數初始化

設定高度與焦點

1
2
3
4
5
6
7
$( '.summernote' ).summernote({
   height: 300,                  // set editor height
 
   minHeight:  null ,              // set minimum height of editor
   maxHeight:  null ,              // set maximum height of editor
 
   focus:  true ,                  // set focus to editable area after initializing summernote});

設定高度后,如果內容高度超過設定高度將出現滾動條,如果沒有設定高度則一直往下掙開。設定focus為true時,打開頁面后焦點定位到編輯器中。

自定義工具欄

1
2
3
4
5
6
7
8
9
10
11
12
$( '.summernote' ).summernote({
   toolbar: [
     //[groupname, [button list]]
      
     [ 'style' , [ 'bold' 'italic' 'underline' 'clear' ]],
     [ 'font' , [ 'strikethrough' ]],
     [ 'fontsize' , [ 'fontsize' ]],
     [ 'color' , [ 'color' ]],
     [ 'para' , [ 'ul' 'ol' 'paragraph' ]],
     [ 'height' , [ 'height' ]],
   ]
});

預設參數

類型

方法
picture Insert a picture
link Insert a hyperlink
video Insert a video
table Insert a table
hr Insert a horizontal rule
style Format selected block
fontname Set font family
fontsize Set font size
color Set foreground and background color
bold Toggle weight
italic Toggle italic
underline Toggle underline
strikethrough Toggle strikethrough
clear Clearing all styles
ul Make an un-ordered list
ol Make an ordered list
paragraph Set text alignment
height Set height of text
fullscreen Toggle fullscreen editing mode
codeview Toggle wysiwyg and html editing mode
undo Undo
redo Redo
help Show help dialog

極簡模式Air-mode

1
2
3
4
5
6
7
8
9
$( '.summernote' ).summernote({
   airPopover: [
     [ 'color' , [ 'color' ]],
     [ 'font' , [ 'bold' 'underline' 'clear' ]],
     [ 'para' , [ 'ul' 'paragraph' ]],
     [ 'table' , [ 'table' ]],
     [ 'insert' , [ 'link' 'picture' ]]
   ]
});

釋放Summernote

1
$( '.summernote' ).destroy();

取值與賦值

1
2
3
4
5
6
//取值
var  sHTML = $( '.summernote' ).code();
//同一頁面多個summernote時,取第二個的值
var  sHTML = $( '.summernote' ).eq(1).code();
//賦值
$( '.summernote' ).code(sHTML);

事件

oninit

1
2
3
4
5
$( '#summernote' ).summernote({
   oninit:  function () {
     console.log( 'Summernote is launched' );
   }
});

onenter

1
2
3
4
5
$( '#summernote' ).summernote({
   onenter:  function (e) {
     console.log( 'Enter/Return key pressed' );
   }
});

onfocus

1
2
3
4
5
$( '#summernote' ).summernote({
   onfocus:  function (e) {
     console.log( 'Editable area is focused' );
   }
});

onblur

1
2
3
4
5
$( '#summernote' ).summernote({
   onblur:  function (e) {
     console.log( 'Editable area loses focus' );
   }
});

onkeyup

1
2
3
4
5
$( '#summernote' ).summernote({
   onkeyup:  function (e) {
     console.log( 'Key is released:' , e.keyCode);
   }
});

onkeydown

1
2
3
4
5
$( '#summernote' ).summernote({
   onkeydown:  function (e) {
     console.log( 'Key is pressed:' , e.keyCode);
   }
});

onpaste

1
2
3
4
5
$( '#summernote' ).summernote({
   onpaste:  function (e) {
     console.log( 'Called event paste' );
   }
});

onImageUpload

可以重寫圖片上傳句柄

1
2
3
4
5
$( '#summernote' ).summernote({
   onImageUpload:  function (files, editor, $editable) {
     console.log( 'image upload:' , files, editor, $editable);
   }
});

onChange

IE9-10: DOMCharacterDataModified, DOMSubtreeModified, DOMNodeInserted

Chrome, FF: input

1
2
3
4
5
$( '#summernote' ).summernote({
   onChange:  function (contents, $editable) {
     console.log( 'onChange:' , contents, $editable);
   }
});

支持18國語言,使用時引入你需要的語言文件,lang值設為你需要的語言

1
2
3
4
5
6
7
8
<!-- include summernote-ko-KR -->
<script src= "lang/summernote-ko-KR.js" ></script>
 
$(document).ready( function () {
   $( '#summernote' ).summernote({
     lang:  'ko-KR'  // default: 'en-US'
   });
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM