一直想给评论加上简易的编辑器,最近正好有心情折腾一下,研究了一下,现在把实现的方法和大家分享(本文参考了《为评论框添加简单文字编辑器》这篇文章的绝大部分代码,并修改了一些错误,增加了点功能)。

首先在head或者footer加入下面jQuery代码:

/* Comment Editor */
$(function() {
    function addEditor(a, b, c) {
        if (document.selection) {
            a.focus();
            sel = document.selection.createRange();
            c ? sel.text = b + sel.text + c: sel.text = b;
            a.focus()
        } else if (a.selectionStart || a.selectionStart == '0') {
            var d = a.selectionStart;
            var e = a.selectionEnd;
            var f = e;
            c ? a.value = a.value.substring(0, d) + b + a.value.substring(d, e) + c + a.value.substring(e, a.value.length) : a.value = a.value.substring(0, d) + b + a.value.substring(e, a.value.length);
            c ? f += b.length + c.length: f += b.length - e + d;
            if (d == e && c) f -= c.length;
            a.focus();
            a.selectionStart = f;
            a.selectionEnd = f
        } else {
            a.value += b + c;
            a.focus()
        }
    }
    var g = document.getElementById('comment') || 0;
    var h = {
        strong: function() {
            addEditor(g, '<strong>', '</strong>')
        },
        em: function() {
            addEditor(g, '<em>', '</em>')
        },
        del: function() {
            addEditor(g, '<del>', '</del>')
        },
        underline: function() {
            addEditor(g, '<u>', '</u>')
        },
        quote: function() {
            addEditor(g, '
<blockquote>', '</p></blockquote>
<p>')
        },
        ahref: function() {
            var a = prompt('请输入链接地址', 'http://');
            var b = prompt('请输入链接描述','');
            if (a) {
                addEditor(g, '<a target="_blank" href="' + a + '"rel="external">' + b + '</a>','')
            }
        },
        img: function() {
            var a = prompt('请输入图片地址', 'http://');
            if (a) {
                addEditor(g, '<img src="' + a + '" alt="" />','')
            }
        },
        code: function() {
            addEditor(g, '<code>', '</code>')
        }
    };
    window['SIMPALED'] = {};
    window['SIMPALED']['Editor'] = h
});

HTML代码:

<div id="editor_tools">
   <a href="javascript:SIMPALED.Editor.strong()">粗体</a>
    <a href="javascript:SIMPALED.Editor.em()">斜体</a>
    <a href="javascript:SIMPALED.Editor.del()">删除线</a>
    <a href="javascript:SIMPALED.Editor.underline()">下划线</a>
    <a href="javascript:SIMPALED.Editor.ahref()">链接</a>
    <a href="javascript:SIMPALED.Editor.code()">插代码</a>
    <a href="javascript:SIMPALED.Editor.quote()">引用</a>
    <a href="javascript:SIMPALED.Editor.img()">插图</a>
</div>

PS:如果主题使用了wordpress3.0以上提供的数组描述来写的,应根据具体情况适当修改。

最后将下列样式加入样式表:

/* editor_tools */
.editor_tools{margin:0;padding:0;width:360px;height:20px;border-left:1px solid #ddd;}
.editor_tools a{float:left;color:#999;height:20px;line-height:20px;padding:0 5px;font-weight:bold;background:#fafafa;border-top:1px solid .ddd;border-right:1px solid #ddd;}
.editor_tools a:hover{background:#fff;}

好了,这样一个简洁但很好用的评论编辑器就加入成功了,赶快试试吧!

Last modification:March 5, 2020
If you think my article is useful to you, please feel free to appreciate