我将提供解决节点大小限制问题的第一种方法。我修改了 unescapeHtml 代码,通过创建一个循环遍历每个 childNode 并将 nodeValues 附加到结果的循环来处理 Firefox 将数据拆分为多个节点。然后该函数在Matt Thommes 的简洁删除所有子节点代码的帮助下清理节点
我的代码如下:
//htmlToCKEditor
// Arguments:
// 1. contentCK - Escaped content (likely HTML) to be inserted into CKEditor. (string)
// 2. targetCK- The ID of the target CKEditor instance. (string)
// Creates a temporary element, inserts the escaped content into the innerHTML,
// loops over every childNode, appends each nodeValue to the result string,
// removes the temporary element's child nodes, sets the data of the CKEditor to the new unescaped value.
function htmlToCKEditor (contentCK,targetCK) {
// Return false if there's no target CKEditor instance
if (!(targetCK.match(/\w+/))){
return false;
}
if (contentCK.match(/\w+/)){
var thisHTML = unescape(contentCK);
var temp = document.createElement("div");
temp.innerHTML = thisHTML;
// Loop over childNodes and append values to result variable.
var result = '';
for (var i=0; i < temp.childNodes.length; i++){
result += temp.childNodes[i].nodeValue;
}
// Cleanup from Matt Thommes
if (temp.hasChildNodes()){
while (temp.childNodes.length >= 1){
temp.removeChild(temp.firstChild);
}
}
// Set CKEditor instance of choice with the newly unescaped result.
CKEDITOR.instances[targetCK].setData(result);
}else{
CKEDITOR.instances[targetCK].setData('');
}
return true;
}
可以采取多种替代方法来完成此任务,因此我改进了我的问题并将我的代码拆分为答案。我很想阅读这个问题的更多解决方案,因为我对 CKEditor 方法并不完全熟悉,而且 CKEditor 可能内置了一个更简单的解决方案。