Javascript 换行符在window.open()中不起作用。文档

Javascript 换行符在window.open()中不起作用。文档,javascript,css,Javascript,Css,我试图在javascript中的window.open.document中使用换行符\n。这不符合预期。\反斜杠基本上导致了一个问题,它就像是对我的代码的一个中断。我必须有这部分代码来获得换行符的计数,并替换不必要的换行符。如何使\被视为字符串 我曾经尝试过一些称呼,比如通过引用解决方案添加四个反斜杠\\,但没有改变 printWindow=window.open,,location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,r

我试图在javascript中的window.open.document中使用换行符\n。这不符合预期。\反斜杠基本上导致了一个问题,它就像是对我的代码的一个中断。我必须有这部分代码来获得换行符的计数,并替换不必要的换行符。如何使\被视为字符串

我曾经尝试过一些称呼,比如通过引用解决方案添加四个反斜杠\\,但没有改变

printWindow=window.open,,location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizeable=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;');

// 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('}');

我希望输出可以考虑和/或执行它,如果我尝试在打印时使用相同的代码。文档工作正常。

尝试使用。它工作得很好


需要转义才能忽略它。当我在控制台中运行它时,可以在输出中看到\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('}');

所以你需要逃避它,所以一个地方应该是/\\r\\n |\\n | \\rI已经尝试添加\\但它对甲烷不起作用它起作用很好它得到了解决我用了两个斜杠\