Javascript tostring问题
我正在开发一个所见即所得文本编辑器,我正在开发在所见即所得模式和BBcode模式之间切换的功能。在您切换后,我很难让它以WYSIWYG模式显示Javascript tostring问题,javascript,Javascript,我正在开发一个所见即所得文本编辑器,我正在开发在所见即所得模式和BBcode模式之间切换的功能。在您切换后,我很难让它以WYSIWYG模式显示 function editormode() { var html; var bbcode = new Array(); var htmlcode = new Array(); htmlcode[0] = "<b>"; bbcode[0] = "[b]"; htmlcode[1] = "</b>"; bbc
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";
}
}
函数编辑器模式()
{
var-html;
var bbcode=新数组();
var htmlcode=新数组();
htmlcode[0]=“bbcode[0]=“b]”;
htmlcode[1]=”;bbcode[1]=“[/b]”;
htmlcode[2]=“bbcode[2]=“i]”;
htmlcode[3]=”;bbcode[3]=“[/i]”;
htmlcode[4]=“bbcode[4]=“u]”;
htmlcode[5]=”;bbcode[5]=“[/u]”;
htmlcode[6]=”;bbcode[6]=”[strike]”;
htmlcode[7]=”;bbcode[7]=”[/strike]”;
htmlcode[8]=”;bbcode[8]=”[sub]”;
htmlcode[9]=”;bbcode[9]=”[/sub]”;
htmlcode[10]=”;bbcode[10]=”[sup]”;
htmlcode[11]=”;bbcode[11]=”[/sup]”;
如果(编辑模式==“真”){
htmltext=document.getElementById('editor').contentWindow.document.body.innerHTML;
对于(i=0;i<12;i++){
searchtext=htmltext.search(htmlcode[i]);
如果(搜索文本!=-1){
htmltext=htmltext.replace(htmlcode[i],bbcode[i]);
}
}
html=document.createTextNode(htmltext);
document.getElementById(“编辑器”).contentWindow.document.body.innerHTML=“”;
html=document.getElementById('editor').contentWindow.document.importNode(html,false);
document.getElementById('editor').contentWindow.document.body.appendChild(html);
editormode=“false”;
}否则{
htmltext=document.getElementById('editor').contentWindow.document.body.innerHTML;
对于(i=0;i<12;i++){
searchtext=htmltext.search(bbcode[i]);
如果(搜索文本!=-1){
htmltext=htmltext.replace(bbcode[i],htmlcode[i]);
}
}
html=document.createTextNode(htmltext);
document.getElementById(“编辑器”).contentWindow.document.body.innerHTML=“”;
html=document.getElementById('editor').contentWindow.document.importNode(html,false);
document.getElementById('editor').contentWindow.document.body.appendChild(html);
editormode=“true”;
}
}
如果我看到的是正确的,那么看起来您正在对整个文本体执行String.replace()
,并且仅使用字符串查找要替换的目标
我怀疑正在发生的是,您只是在替换您试图查找和替换的标记的第一个实例。当在传递字符串时使用String.replace()
作为搜索参数时,它将只查找并替换匹配子字符串的第一个实例
"hello".replace('l', 'r'); // returns "herlo" and not "herro"
在执行替换之前,请尝试将搜索字符串更改为全局正则表达式:
var tagEx = new RegExp(htmlcode[i], 'g');
htmltext.replace(tagEx, bbcode[i]);
我相信这会解决你的问题。此外,在执行此操作时,您可能不需要预先调用String.search()
。对不匹配的字符串调用String.replace()
,没有什么不好的。所以,去掉这个检查,甚至可以节省一些计算时间
另一件值得一提的事情是,我不确定这是否与代码的其余部分冲突,即当您执行数组循环时,您没有使用var
来实例化增量变量。如var i=0
,这有时会导致问题,因为不使用var
将创建一个全局变量,该变量可能与其他地方的代码冲突
希望这能有所帮助。您的函数有几个问题,但是字符串替换问题可能是导致它看起来没有转换任何内容的错误。 以下是适用于我的函数的较小版本:
function editormode(is_editor_mode) {
var replaceTagsByMode = function(html, is_editor_mode) {
var tags = {};
for (var i=0, a=['b', 'i', 'u', 'strike', 'sub', 'sup']; i<a.length; i++) {
tags[['<', a[i], '>'].join('')] = ['[', a[i], ']'].join('');
tags[['</', a[i], '>'].join('')] = ['[/', a[i], ']'].join('');
}
for (var html_tag in tags) {
if (tags.hasOwnProperty(html_tag)) {
html = html.replace.apply(
html, is_editor_mode ? [html_tag, tags[html_tag], 'g'] : [tags[html_tag], html_tag, 'g']);
}
}
return html;
};
var editor_body = document.getElementById('editor').contentWindow.document.body;
editor_body.innerHTML = replaceTagsByMode(editor_body.innerHTML, is_editor_mode);
}
函数编辑器模式(即编辑器模式){
var replaceTagsByMode=函数(html,是编辑器模式){
var标签={};
对于(var i=0,a=['b','i','u','strike','sub','sup'];i最好使用现有代码来实现这一点,例如(仅用于将bbcode转换为HTML)或(用于整个编辑器)。另请参阅
如果您愿意自己编写,转换这些标记可以非常简单:
html = bbcode.replace(/\[(\/?(b|i|u|strike|sub|sup))\]/gi, '<$1>');
bbcode = html.replace(/<(\/?(b|i|u|strike|sub|sup))>/gi, '[$1]');
html=bbcode.replace(/\[(\/?(b | i | u | strike | sub | sup))\]/gi';
bbcode=html.replace(//gi,[$1]');
我们可以看到您将editormode设置为false的代码吗?忽略此项。我认为底部的editormode=true;在else之外。格式很难读取。