我正在研究所见即所得的文本编辑器,并且正在研究在所见即所得模式和BBcode模式之间切换的功能。在进行切换后,我无法使其以所见即所得模式显示。Javascript tostring问题
function editormode()
{
var html;
var bbcode = new Array();
var htmlcode = new Array();
htmlcode[0] = "<b>"; bbcode[0] = "[b]";
htmlcode[1] = "</b>"; bbcode[1] = "[/b]";
htmlcode[2] = "<i>"; bbcode[2] = "[i]";
htmlcode[3] = "</i>"; bbcode[3] = "[/i]";
htmlcode[4] = "<u>"; bbcode[4] = "[u]";
htmlcode[5] = "</u>"; bbcode[5] = "[/u]";
htmlcode[6] = "<strike>"; bbcode[6] = "[strike]";
htmlcode[7] = "</strike>"; bbcode[7] = "[/strike]";
htmlcode[8] = "<sub>"; bbcode[8] = "[sub]";
htmlcode[9] = "</sub>"; bbcode[9] = "[/sub]";
htmlcode[10] = "<sup>"; bbcode[10] = "[sup]";
htmlcode[11] = "</sup>"; bbcode[11] = "[/sup]";
if (editormode == "true") {
htmltext = document.getElementById('editor').contentWindow.document.body.innerHTML;
for(i = 0; i < 12; i++){
searchtext = htmltext.search(htmlcode[i]);
if(searchtext != -1) {
htmltext = htmltext.replace(htmlcode[i], bbcode[i]);
}
}
html = document.createTextNode(htmltext);
document.getElementById('editor').contentWindow.document.body.innerHTML = "";
html = document.getElementById('editor').contentWindow.document.importNode(html,false);
document.getElementById('editor').contentWindow.document.body.appendChild(html);
editormode = "false";
} else {
htmltext = document.getElementById('editor').contentWindow.document.body.innerHTML;
for(i = 0; i < 12; i++){
searchtext = htmltext.search(bbcode[i]);
if(searchtext != -1) {
htmltext = htmltext.replace(bbcode[i], htmlcode[i]);
}
}
html = document.createTextNode(htmltext);
document.getElementById('editor').contentWindow.document.body.innerHTML = "";
html = document.getElementById('editor').contentWindow.document.importNode(html,false);
document.getElementById('editor').contentWindow.document.body.appendChild(html);
editormode = "true";
}
}
我们可以看到代码在您设置editormode为false。忽略这个。我认为editormode = true;底部是在其他地方。格式化很难阅读。 – 2010-08-07 17:12:03