JavaScript 实现文本编辑器
JavaScript 实现文本编辑器
最近,我需要做一个非常基本的网页内容编辑功能。我不想使用 iframe ,我也不想要一个功能特别多的复杂编辑器,只需要很基本的内容编辑功能,例如粗体,斜体,列表,对齐等等。
用谷歌搜索找了很久,发现所有的插件都是功能太复杂,不是我想要的。所以,我决定我自己来实现需要的编辑功能。刚开始我觉得应该要花费很多的时间,因为我想象内容编辑功能应该是很复杂的。
但事实证明,它是如此简单,让我十分惊讶!借助 HTML5 特性,所有的工具都已经可用,所有你需要做的只是配合他们编写一些非常简单的 JavaScript 代码调用就可以了。
你需要两样东西,第一是:
1
|
contenteditable |
contenteditable 是 HTML 中的一个属性,设置 HTML标签的 contenteditable=“true” 时,即可开启该元素的编辑模式。contenteditable 的作用相当神奇,可以让 div 或整个网页,以及 span 等等元素设置为可编辑。
我们最常用的输入文本内容是 input 与 textarea,使用 contenteditable 属性后,可以在div、table、p、span、body等等很多元素中输入内容。如果想要整个网页可编辑,可以在 body 标签内设置 contenteditable。contenteditable 已在 HTML5 标准中得到有效的支持.
第二样东西是一个功能特殊的函数:
1
|
execCommand |
execCommand 让我们能够对 contenteditable 区域的内容做一些相当复杂的操作。如果你想了解更为详细的内容可以看这里:The Mozilla Docs。
从本质上讲, execCommand 有三个参数:
Command Name – 命令名称;
ShowDefaultUI – 未实现,所以最好设置为 false;
ValueArgument – 命令的参数;
多数情况下,ValueArgument 可以设置为 null,但有一种情况:当你要设置一行文本的标签的时候(h1,h2,p 等),你需要使用 formatBlock 命令,并把标签放到 ValueArgument 中。
现在,事情就很简单了,我们把 exexCommand 要执行的命令放到 data-role 属性中,然后编写简单的 JavaScript 都可以很容易地完成编辑器功能了。核心代码其实就10行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$( function () { $( '#editControls a' ).click( function (e) { switch ($( this ).data( 'role' )) { case 'h1' : case 'h2' : case 'p' : document.execCommand( 'formatBlock' , false , '<' + $( this ).data( 'role' ) + '>' ); break ; default : document.execCommand($( this ).data( 'role' ), false , null ); break ; } }) }); |
完整代码如下:
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>Editable WYSIWYG</
title
>
<
link
href
=
"bootstrap.css"
rel
=
"stylesheet"
>
<
link
href
=
"font-awesome.css"
rel
=
"stylesheet"
>
<
style
>
#editor {
resize:vertical;
overflow:auto;
border:1px solid silver;
border-radius:5px;
min-height:100px;
box-shadow: inset 0 0 10px silver;
padding:1em;
}
</
style
>
</
head
>
<
body
>
<
div
class
=
"content"
>
<
div
class
=
"container-fluid"
>
<
div
id
=
'pad-wrapper'
>
<
div
id
=
"editparent"
>
<
div
id
=
'editControls'
class
=
'span12'
style
=
'text-align:center; padding:5px;'
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'undo'
href
=
'#'
><
i
class
=
'icon-undo'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'redo'
href
=
'#'
><
i
class
=
'icon-repeat'
></
i
></
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'bold'
href
=
'#'
><
b
>Bold</
b
></
a
>
<
a
class
=
'btn'
data-role
=
'italic'
href
=
'#'
><
em
>Italic</
em
></
a
>
<
a
class
=
'btn'
data-role
=
'underline'
href
=
'#'
><
u
><
b
>U</
b
></
u
></
a
>
<
a
class
=
'btn'
data-role
=
'strikeThrough'
href
=
'#'
><
strike
>abc</
strike
></
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'justifyLeft'
href
=
'#'
><
i
class
=
'icon-align-left'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'justifyCenter'
href
=
'#'
><
i
class
=
'icon-align-center'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'justifyRight'
href
=
'#'
><
i
class
=
'icon-align-right'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'justifyFull'
href
=
'#'
><
i
class
=
'icon-align-justify'
></
i
></
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'indent'
href
=
'#'
><
i
class
=
'icon-indent-right'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'outdent'
href
=
'#'
><
i
class
=
'icon-indent-left'
></
i
></
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'insertUnorderedList'
href
=
'#'
><
i
class
=
'icon-list-ul'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'insertOrderedList'
href
=
'#'
><
i
class
=
'icon-list-ol'
></
i
></
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'h1'
href
=
'#'
>h<
sup
>1</
sup
></
a
>
<
a
class
=
'btn'
data-role
=
'h2'
href
=
'#'
>h<
sup
>2</
sup
></
a
>
<
a
class
=
'btn'
data-role
=
'p'
href
=
'#'
>p</
a
>
</
div
>
<
div
class
=
'btn-group'
>
<
a
class
=
'btn'
data-role
=
'subscript'
href
=
'#'
><
i
class
=
'icon-subscript'
></
i
></
a
>
<
a
class
=
'btn'
data-role
=
'superscript'
href
=
'#'
><
i
class
=
'icon-superscript'
></
i
></
a
>
</
div
>
</
div
>
<
div
id
=
'editor'
class
=
'span12'
style
=
''
contenteditable>
<
h1
>This is a title!</
h1
>
<
p
>This is just some example text to start us off</
p
>
</
div
>
</
div
>
</
div
>
</
div
>
</
div
>
<
script
src
=
"jquery.min.js"
></
script
>
<
script
src
=
"bootstrap.min.js"
></
script
>
<
script
>
$(function() {
$('#editControls a').click(function(e) {
switch($(this).data('role')) {
case 'h1':
case 'h2':
case 'p':
document.execCommand('formatBlock', false, '<' + $(this).data('role') + '>');
break;
default:
document.execCommand($(this).data('role'), false, null);
break;
}
})
});
</
script
>
</
body
>
</
html
>
JavaScript 实现文本编辑器的更多相关文章
- 10个免费的javascript富文本编辑器(jQuery and non-jQuery)
祝愿园子里的朋友圣诞节快乐. 本文介绍了10个免费易用富文本编辑器(rich text editors,RTE),其中5个是Jquery插件,另外5个是非Jquery富文本编辑器 简介 Javascr ...
- Javascript富文本编辑器
分享几款Javascript富文本编辑器 ueditor jqframework xheditor htmlbox kindeditor wymeditor jhtmlarea markitup ck ...
- javascript 在线文本编辑器
javascript 在线文本编辑器实现代码. 效果例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGhwZmVuZ2h1bw==/font/5 ...
- javascript 简易文本编辑器
转载请注明出处:http://www.cnblogs.com/enzozo/p/4357031.html 写在前面: 本文本编辑器具备功能:选择字体大小.颜色.加粗.斜体.下划线.点击 'Submit ...
- JavaScript 富文本编辑器
WEB项目中使用UEditor(富文本编辑器) UEditor - 完整示例 http://ueditor.baidu.com/website/onlinedemo.html UEditor注意事项: ...
- 小伙伴们惊呆了!10行 JavaScript 实现文本编辑器
最近,我需要做一个非常基本的网页内容编辑功能.我不想使用 iframe ,我也不想要一个功能特别多的复杂编辑器,只需要很基本的内容编辑功能,例如粗体,斜体,列表,对齐等等. 您可能感兴趣的相关文章 分 ...
- 10行 JavaScript 实现文本编辑器
背景 我们平时用到的浏览器编辑器功能都会比较多,实现的代码逻辑也会非常复杂,往往是作为一个单独插件被引入进来的.但是,现在我只需要一个很基本的内容输入内容编辑的功能,如:粗体.斜体.列表.对齐等.那要 ...
- Thinkphp 文本编辑器
文本编辑器:可以从网上下载---ueditor文件夹里面包含php和utf8-php两个文件夹 平时使用时主要用到获取内容和写入内容两个按钮 获取内容: <!DOCTYPE html PUBLI ...
- 富文本编辑器TinyMCE
最近项目中用到了javascript富文本编辑器,从网上找开源控件,发现很多可选,参考下面文章,列出了很多可用的插件http://www.cnblogs.com/ywqu/archive/2009/1 ...
随机推荐
- boost进程间通信经常使用开发一篇全(消息队列,共享内存,信号)
本文概要: 敏捷开发大家想必知道并且评价甚高,缩短开发周期,提高开发质量.将大project独立为不同的小app开发,整个开发过程,程序可用可測,所以提高了总体的质量.基于这样的开发模式和开发理念,进 ...
- sql server int 列 NULLIF,isnull 判断是0还是1 ,如果是0就变成1
SELECT ISNULL(NULLIF(col1,0),1) ISNULL: 第一个表达式 是 null 返回 第二个表达式,否则 返回 第一个 , ISNULL(表达式1,表达式2) if(表达 ...
- fast-json.jar的用法
fast-json.jar 解析json数据:一种json数据解析方式是这种,点击这里下载jsonfast.jar+fastjsonAPI文档 [ { "id": 6378, &q ...
- 项目管理实践 -- 健身小管家(Fitness housekeeper)的管理(4)
提前几天把检查更新的功能完成了.
- 10个实用的PHP正则表达式汇总
原文 10个实用的PHP正则表达式汇总 正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本 ...
- 文档PDF开放
108个大数据文档PDF开放下载 投递人 itwriter 发布于 2015-01-29 13:34 评论(13) 有2251人阅读 原文链接 [收藏] « » 文/36 大数据 总有人问我 ...
- PAT 1006. Sign In and Sign Out
#include<iostream> #include<string> using namespace std; int main(){ int cnt;cin>> ...
- win7 Python 环境 准备 配置
包括Python,eclipse,jdk,pydev,pip,setuptools,beautifulsoup,pyyaml,nltk,mysqldb的下载安装配置. **************** ...
- 推荐一个比较好的VBS编辑器
QTP 本身的IDE环境, 有诸多缺陷.所以,一般中级以上的自动化测试工程师都会采用外部其他编辑器来编辑VBS脚本.通常情况下,一般都 notepad++. 但是,notepad++也是有很多不足之处 ...
- ASP.NET MVC IOC之Unity攻略
ASP.NET MVC IOC之Unity攻略 一.你知道IOC与DI吗? 1.IOC(Inversion of Control )——控制反转 即依赖对象不在被依赖模块的类中直接通过new来获取 先 ...