换行符在window.open()。document内部不起作用

时间:2019-07-11 17:23:17

标签: javascript css

我正在尝试在javascript的window.open()。document内使用换行符(“ \ n”)。这没有按预期工作。反斜杠“ \”基本上会引起问题,因为它会破坏我的代码。我必须具有这部分代码以获取换行符的数量,并替换不必要的换行符。如何使“ \”被视为字符串?

我已经尝试过采用解决方案,例如通过引用解决方案添加四个反斜杠(\\),但它没有任何变化Can't escape the backslash with regex?

printWindow = window.open(“”,“”,“位置=否,菜单栏=否,目录=否,滚动条=是,工具栏=否,可调整大小=是”,false);

printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');

// this is where my code is getting failed because of the "\" used

printWindow.document.write('textareaContent = textareaContent.replace(/(\r\n|\n|\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');

printWindow.document.write('var maxrows=300; ');

//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');

// also here

printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\n");');

printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');

printWindow.document.write('}');
printWindow.document.write('}');

如果我在printwindow.document之外尝试相同的代码,我希望输出考虑“ /”并执行它。

2 个答案:

答案 0 :(得分:1)

需要被忽略才能忽略。在控制台中运行以下命令时,在输出中看到\r\n|\n|\r

printWindow=window.open("","","location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizable=yes",false);

printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');

printWindow.document.write('textareaContent = textareaContent.replace(/(\\r\\n|\\n|\\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');

printWindow.document.write('var maxrows=300; ');

//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');

// also here

printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\\\n");');

printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');

printWindow.document.write('}');
printWindow.document.write('}');

答案 1 :(得分:0)

尝试改用<br>。效果很好。

see working code