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 ...
随机推荐
- 如何快速正确的安装 Ruby, Rails 运行环境
如何快速正确的安装 Ruby, Rails 运行环境 https://ruby-china.org/wiki/install_ruby_guide 对于新入门的开发者,如何安装 Ruby, Ruby ...
- MySQL编程(0) - Mysql中文乱码问题解决方案
MySQL 5.6 for Windows 解压缩版配置安装: http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL ...
- PHP 使用用户自定义的比较函数对数组中的值进行排序
原文:PHP 使用用户自定义的比较函数对数组中的值进行排序 usort (PHP 4, PHP 5) usort — 使用用户自定义的比较函数对数组中的值进行排序 说明 bool ...
- ASP.NET如何显示农历时间
ASP.NET如何显示农历时间 CS部分代码如下: 代码如下: public string ChineseTimeNow = ""; public string ForignTi ...
- Robotium源码分析之运行原理
从上一章<Robotium源码分析之Instrumentation进阶>中我们了解到了Robotium所基于的Instrumentation的一些进阶基础,比如它注入事件的原理等,但Rob ...
- ThinkPad E431/E531 ubuntu 14.04 安装无线网卡驱动
ubuntu系统装好之后无限网卡驱动并不能用.须要自己下载无线网卡驱动.以下是最简单的方法: sudo apt-get install linux-headers-generic build-esse ...
- QT Creater与libusb使用
新建一个C项目,然后修改.pro文件,添加LIBS一行 TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt LIBS ...
- 开发者需要了解的WebKit
2013-3-22 22:37| 发布者: sxwgf| 查看: 575| 评论: 0|来自: infoq 摘要: Paul Irish是著名的前端开发工程师,同时他也是Chrome开发者关系团队成员 ...
- 【c++类的构造函数具体解释
】
一.构造函数是干什么的 class Dog { public: // 类Dog的构造函数 // 特点:以类名作为函数名,无返回类型 Dog() ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...