bootstrap-wysiwyg--富文本编辑器使用教程

这个jQuery Bootstrap小插件(5KB, < 200 行代码)可以将任何一个DIV转变成一个WYSIWYG富文本编辑器,灵感来自于 CLEditor 和 bootstrap-wysihtml5.
使用如下:
1、引入的CSS和JS

这些文件在bootstrap-wysiwyg 中有

<link rel="stylesheet" href="/stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.no-icons.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css">
<link rel="stylesheet" href="index.css" type="text/css">
<script src="/javascripts/jquery.min.js"></script>
<script src="/javascripts/bootstrap.min.js"></script>
<script src="/javascripts/bootstrap-wysiwyg.js"></script>
<script src="/javascripts/jquery.hotkeys.js"></script>

2、HTML代码

<div class="container">
    <div style="margin-top:50px" class="input-group input-group-lg"><span class="input-group-addon"></span><input
            type="text" placeholder="标题" name="title" class="form-control"></div>
    <div style="margin-top:20px" data-role="editor-toolbar" data-target="#editor" class="btn-toolbar">
        <div class="btn-group"><a data-toggle="dropdown" title="Font" class="btn dropdown-toggle"><i
                class="glyphicon glyphicon-font"></i><b class="caret"></b></a>
            <ul class="dropdown-menu"></ul>
        </div>
        <div class="btn-group"><a data-toggle="dropdown" title="Font Size" class="btn dropdown-toggle"><i
                class="glyphicon glyphicon-text-height"></i><b class="caret"></b></a>
            <ul class="dropdown-menu">
                <li><a data-edit="fontSize 5"><font size="5">Huge</font></a></li>
                <li><a data-edit="fontSize 3"><font size="3">Normal</font></a></li>
                <li><a data-edit="fontSize 1"><font size="1">Small</font></a></li>
            </ul>
        </div>
        <div class="btn-group"><a data-edit="bold" title="Bold (Ctrl/Cmd+B)" class="btn"><i class="icon-bold"></i></a><a
                data-edit="italic" title="Italic (Ctrl/Cmd+I)" class="btn"><i class="icon-italic"></i></a><a
                data-edit="strikethrough" title="Strikethrough" class="btn"><i class="icon-strikethrough"></i></a><a
                data-edit="underline" title="Underline (Ctrl/Cmd+U)" class="btn"><i class="icon-underline"></i></a>
        </div>
        <div class="btn-group"><a data-edit="insertunorderedlist" title="Bullet list" class="btn"><i
                class="icon-list-ul"></i></a><a data-edit="insertorderedlist" title="Number list" class="btn"><i
                class="icon-list-ol"></i></a><a data-edit="outdent" title="Reduce indent (Shift+Tab)" class="btn"><i
                class="icon-indent-left"></i></a><a data-edit="indent" title="Indent (Tab)" class="btn"><i
                class="icon-indent-right"></i></a></div>
        <div class="btn-group"><a data-edit="justifyleft" title="Align Left (Ctrl/Cmd+L)" class="btn"><i
                class="icon-align-left"></i></a><a data-edit="justifycenter" title="Center (Ctrl/Cmd+E)" class="btn"><i
                class="icon-align-center"></i></a><a data-edit="justifyright" title="Align Right (Ctrl/Cmd+R)"
                                                     class="btn"><i class="icon-align-right"></i></a><a
                data-edit="justifyfull" title="Justify (Ctrl/Cmd+J)" class="btn"><i class="icon-align-justify"></i></a>
        </div>
        <div class="btn-group"><a data-toggle="dropdown" title="Hyperlink" class="btn dropdown-toggle"><i
                class="icon-link"></i></a>

            <div class="dropdown-menu input-append"><input placeholder="URL" type="text" data-edit="createLink"
                                                           class="span2">
                <button type="button" class="btn">Add</button>
            </div>
            <a data-edit="unlink" title="Remove Hyperlink" class="btn"><i class="icon-cut"></i></a></div>
        <div class="btn-group"><a id="pictureBtn" title="Insert picture (or just drag &amp; drop)" class="btn"><i
                class="icon-picture"></i></a><input type="file" data-role="magic-overlay" data-target="#pictureBtn"
                                                    data-edit="insertImage">

            <div class="btn-group"><a data-edit="undo" title="Undo (Ctrl/Cmd+Z)" class="btn"><i
                    class="icon-undo"></i></a><a data-edit="redo" title="Redo (Ctrl/Cmd+Y)" class="btn"><i
                    class="icon-repeat"></i></a></div>
            <input id="voiceBtn" type="text" data-edit="inserttext" x-webkit-speech=""></div>
    </div>
    <div id="editor">Go ahead&hellip;</div>

3、jquery代码

$(function () {
        function initToolbarBootstrapBindings() {
            var fonts = ['Serif', 'Sans', 'Arial', 'Arial Black', 'Courier',
                        'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande', 'Lucida Sans', 'Tahoma', 'Times',
                        'Times New Roman', 'Verdana'],
                    fontTarget = $('[title=Font]').siblings('.dropdown-menu');
            $.each(fonts, function (idx, fontName) {
                fontTarget.append($('<li><a data-edit="fontName ' + fontName + '" style="font-family:\'' + fontName + '\'">' + fontName + '</a></li>'));
            });
            $('a[title]').tooltip({container: 'body'});
            $('.dropdown-menu input').click(function () {
                return false;
            }).change(function () {
                $(this).parent('.dropdown-menu').siblings('.dropdown-toggle').dropdown('toggle');
            }).keydown('esc', function () {
                this.value = '';
                $(this).change();
            });

            $('[data-role=magic-overlay]').each(function () {
                var overlay = $(this), target = $(overlay.data('target'));
                overlay.css('opacity', 0).css('position', 'absolute').offset(target.offset()).width(target.outerWidth()).height(target.outerHeight());
            });
            $('#voiceBtn').hide();

        };
        initToolbarBootstrapBindings();
        $('#editor').wysiwyg();
        window.prettyPrint && prettyPrint();
    });

4、界面如下:
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值