TinyMCE - 在底部插入内容

时间:2011-11-01 04:29:14

标签: javascript tinymce

我知道我们可以通过这种方式将代码插入到顶部。

ed.selection.setCursorLocation(ed.getBody()。firstChild,0);

但是,我不确定如何在底部实现内容。

2 个答案:

答案 0 :(得分:4)

您可以使用以下DOMUtils.add()方法将新的HTML元素添加到文档正文中。

例如,以下内容添加了一个CSS类为“text_it”的空段落

ed.dom.add(ed.getBody(), 'p', {'class' : 'text_it'});

答案 1 :(得分:4)

另一种方法:

function getTextNodes(node, nodeType, result){

    var children = node.childNodes;
    var nodeType = nodeType ? nodeType : 3;

    var result = !result ? [] : result;
    if (node.nodeType == nodeType) {
        result.push(node);
    }

    for (var i=0; i<children.length; i++) {
        result = this.getTextNodes(children[i], nodeType, result)
    }

    return result;
};

// get all Textnodes from lastchild, calc length
var textnodes = getTextNodes(ed.getBody().lastChild);

// set Cursor to last position
ed.selection.setCursorLocation(textnodes[textnodes.length-1], textnodes[textnodes.length-1].textContent.length );