我正在使用bootstrap-wysiwyg editor(mindmup)
。在该编辑器工具中,我使用包含模板名称的自定义下拉列表。模板是文本,有些是html。下拉列表的javascript函数如下
function loadTemplate(templateId) {
$.post('/Template/GetTemplateDetails', { id: templateId }, function (data, textStatus) {
if (textStatus == "success") {
insertTextAtCursor(data.PreFilledText);
}
}, "json"); }
insertTextAtCursor功能代码如下
function insertTextAtCursor(text) {
var sel, range, html;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(text));
}
} else if (document.selection && document.selection.createRange) {
document.selection.createRange().text = text;
} }
我的问题是,如果模板是html代码,那么“insertTextAtCursor”函数返回原始html,但它适用于文本。当我在loadTemplate函数中使用$('#editor').html(data.PreFilledText);
而不是insertTextAtCursor(data.PreFilledText);
时,它工作正常,但我无法在我的光标点选择另一个模板。
所以,有人帮我自定义“insertTextAtCursor”函数,该函数使用document.createTextNode来允许编辑文本区域中的文本和html,还是有其他选项?