【发布时间】:2014-04-21 00:37:50
【问题描述】:
我正在尝试将文本文件的内容显示到文本区域元素中,但我遇到了换行符显示为 '\n' 而不是实际执行的问题。
<textarea id = "textArea" name = "textArea" data-ng-model="note.text"></textarea>
javascript 将文本添加到文本区域元素
var $promise=$http.post("http://localhost/PHP/findNotes.php", data); //send data to user.php
$promise.then(function(msg){
document.getElementById("textArea").value = msg.data;
});
获取文件内容的PHP文件
$note=json_decode(file_get_contents('php://input')); //get note
$noteId = ($note->noteId);
$userId = ($note->userId);
$filename = 'file://localhost/Library/WebServer/Documents/Notes/'.$userId.'/'.$noteId.'.txt';
if (file_exists($filename))
{
$file = fopen($filename, "r");
while (!feof($file))
{
$display = fgets($file, filesize($filename));
echo $display;
}
fclose($file);
}
else
{
echo "Error occurred!";
}
读取带内容的文件时
line1\nline2\n\nline4
文本区域显示完全相同的内容
但是,如果文件内容直接应用于 js 代码中的 textarea,那么换行符就可以完美地工作。
所以如果JS改成
document.getElementById("textArea").value ="line1\nline2\n\nline4";
那么文本区域的输出是正确的:
line1
line2
line4
希望得到任何帮助,似乎是从 PHP 接收到的文本格式有问题? 提前致谢
【问题讨论】:
标签: javascript php textarea line-breaks