javascript - 换行符在 window.open().document 中不起作用

标签 javascript css

我正在尝试在 javascript 的 window.open().document 中使用换行符 ("\n")。这没有按预期工作。 “\” 反斜杠基本上导致了它对我的代码起到中断作用的问题。我必须有这部分代码来获取换行符的计数并替换不必要的换行符。如何让“\”被视为字符串?

我引用解决方案尝试了加四个反斜杠(\\)等解决方案,但没有任何变化Can't escape the backslash with regex?

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;');

// 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 之外尝试相同的代码,我希望输出考虑“/”并执行它,它工作正常。

最佳答案

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

关于javascript - 换行符在 window.open().document 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56994435/

相关文章:

javascript - 如何使用 Execute Javascript driver.findElement(Robot Framework 中的 By.xpath

javascript - HTML5 FileReader 如何返回结果?

从 0 到 20 的 5 个随机非重复整数

javascript - Twitter 框不会自动填充报价

javascript - 悬停文本上的 slider

javascript - 如何检查元素是特定元素的子元素?

javascript - 如何根据当前事件日期突出显示元素?

css - 更少的条件/保护来检查元素 :active?

html - 如何使用 CSS 在另一个元素下方添加一个元素?

ruby-on-rails - rails : speed up styling