简体   繁体   English

如何让 PHP 回显“\n”作为 javascript 的纯文本并且没有“\n”创建新行?

[英]How do I let PHP echo “\n” as plain-text for javascript and not have the “\n” create a new line?

PHP is echoing JavaScript (I'm using the jQuery library) something like this: PHP 与 JavaScript 相呼应(我正在使用 jQuery 库)是这样的:

echo 'var users = $("#add").val().split("\n");';

However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript.但是,\n 会在回显脚本的外观中创建换行符,因此会破坏 JavaScript。 Is there a way to circumvent this?有没有办法绕过这个?
Many thanks!非常感谢!

The \n is an escape sequence meaning newline. \n是一个转义序列,意思是换行符。 Backslashes are the beginning of escape sequences, to output a backslash then write \\ .反斜杠是转义序列的开头,到 output 一个反斜杠然后写\\ So you want \\n .所以你想要\\n Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.其他有用的转义序列包括引号:使用\"将引号放入字符串中,而不是结束字符串。

echo "var users = $(\"#add\").val().split(\"\\n\");";

Not sure If you looking for this不确定如果你在找这个

echo "<script>alert('Line1\\\\nThis still in Line1')</script>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM