在NicEdit中的选定文本周围插入html标签 [英] Insert html tags around selected text in NicEdit

查看:142
本文介绍了在NicEdit中的选定文本周围插入html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法在NicEdit中标记的文本周围插入HTML标签,以便我可以缩进文本并使其变为绿色。

I am looking for a way to insert HTML tags around text marked in NicEdit, so that I can, for example, indent the text and make it green.

我想通过插入< / p> pre>在标记的文本周围添加一个我选择的css类的标签(这会对文本进行格式化,使其变成绿色等)。

I want to do this by inserting < pre > tags around the marked text with a css class of my choice(which does the formating of the text, makes it green etc).

我的按钮外观代码如下所示:

The code for my button look as follows:

var customButtonOptions = {
buttons : {
    'code' : {name : __('Mark text as code'), 
                type : 'nicEditorCodeButton'}} , 
                iconFiles : {'code' : '../save.gif'}

                };


var nicEditorCodeButton = nicEditorButton.extend({
mouseClick : function() {

    alert('The code button has been clicked ');
}
});

nicEditors.registerPlugin(nicPlugin,customButtonOptions);

目前我只在函数中有一个提醒,以确保它可以工作,但我需要帮助放置< pre>标签围绕我目前标记的文字。所以不是textarea中的所有文本。

Currently I only have an alert in the function to be sure that it works, but I need help with a function that places the < pre > tags around the text I have currently marked. So not all the text in the textarea.

或者至少是将标记文本放入变量中的一种方法。

Or at least a way of putting the marked text into a variable.

推荐答案

此解决方案适用于我,将标记文本设置为代码。
我希望它适合你。

This solution works for me, to set marked text as code. I hope it will work for you.

if(cmd=='code'){
        if(document.getSelection().anchorNode.data)
        {
            var a = document.getSelection().anchorOffset;
            var b = document.getSelection().focusOffset;
            var str = document.getSelection().anchorNode.data.substring(a,b);
        }
        else str = 'insert code';
        var str1 = '<div class="code">'+str+'</div>';
        function replaceSelectedText(replacementText) {
            var sel, range;
            if (window.getSelection) {
                sel = window.getSelection();
                if (sel.rangeCount) {
                    range = sel.getRangeAt(0);
                    range.deleteContents();
                    document.execCommand('insertHTML',false,replacementText);
                }
            }
        }
        replaceSelectedText(str1);
}

这篇关于在NicEdit中的选定文本周围插入html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆