Insert HTML in NicEdit WYSIWYG

后端 未结 7 1576
忘了有多久
忘了有多久 2020-12-11 06:40

How can I insert text/code at the cursors place in a div created by NicEdit?

I\'ve tried to read the documentation and create my own plugin, but I want it to work wi

7条回答
  •  [愿得一人]
    2020-12-11 07:13

    This function work when nicEdit textarea is empty or cursor is in the blank or new line.

    function addToCursorPosition(textareaId,value) {
                var editor = nicEditors.findEditor(textareaId);
                var range = editor.getRng();
                var editorField = editor.selElm();
                var start = range.startOffset;
                var end = range.endOffset;
                if (editorField.nodeValue != null) {
                    editorField.nodeValue = editorField.nodeValue.substring(0, start) +
                                value +
                                editorField.nodeValue.substring(end, editorField.nodeValue.length);
                }
                else {
                    var content = nicEditors.findEditor(textareaId).getContent().split("
    "); var linesCount = 0; var before = ""; var after = ""; for (var i = 0; i < content.length; i++) { if (linesCount < start) { before += content[i] + "
    "; } else { after += content[i] + "
    "; } linesCount++; if (content[i]!="") { linesCount++; } } nicEditors.findEditor(textareaId).setContent(before + value + after); } }

提交回复
热议问题