所见即所得RichEditor的简单实现

整体思路如下:
  1. 创建一个iframe;
  2. 将designMode设为"on"表示允许编辑;
  3. 点击按钮(可改为图片)时,使用execCommand添加Tag等。
  关于designMode:
  表示是否允许编辑网页内容。IE中对应的是document.body.contentEditable。
以下是一个包含“加粗”、“斜体”和“下划线”三种功能的所见即所得编辑器,当然实际上还算不上“Rich”,不过我们可以采用类似的思路增加其他功能。少数地方用到了jQuery,实际上自己实现也不难。

<input type="button" value="B" οnclick="editor.doc.execCommand('bold')">
<input type="button" value="I" οnclick="editor.doc.execCommand('italic')">
<input type="button" value="U" οnclick="editor.doc.execCommand('underline')">

<br />债务追讨
<iframe id="frame" frameborder="0" style="border:1px solid"></iframe>
<br />
<input type="button" value="Get Value" οnclick="alert($(editor.doc.body).html())">
 
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="editor.js"></script>

editor.js的内容:

editor = {
    init: function() {
        var ie = $.browser.msie;
        editor.doc = $('#frame')[0].contentWindow.document;
   
        if (ie) {
            editor.doc.body.contentEditable = true;
        } else {
            editor.doc.designMode = "on";
        }
    }风之境地 java-javascript 蘑菇街女装
};

$(function() {
    editor.init();
});

转载于:https://www.cnblogs.com/sky7034/archive/2011/05/03/2034987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值