Javascript 使用jQuery'保持导入文本文件的格式;s.load()函数?
我正在将一个文本文件从外部目录导入一个html段落标记中,单击按钮时将显示该标记。我使用jQuery的.load()函数来实现这一点 导入文本文件没有问题,但格式没有保留 导入的文本文件“banks.txt”如下所示:Javascript 使用jQuery'保持导入文本文件的格式;s.load()函数?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在将一个文本文件从外部目录导入一个html段落标记中,单击按钮时将显示该标记。我使用jQuery的.load()函数来实现这一点 导入文本文件没有问题,但格式没有保留 导入的文本文件“banks.txt”如下所示: "Love Poem Serene, In a world full of troubles, i.e. doing nothing about it. - Iain Banks" 而不是: Love Poem Serene, In a world full of troub
"Love Poem Serene, In a world full of troubles, i.e. doing nothing about it. - Iain Banks"
而不是:
Love Poem
Serene,
In a world full of troubles,
i.e. doing nothing about it.
- Iain Banks
有没有办法保持文本文件的格式—换行符、制表符和所有内容?您应该将文本文件作为
pre
元素中的文本加载到pre
标记中
以固定宽度字体显示,并保留空格和空格
换行。对于Internet Explorer,请尝试此操作(其行为不同;对于其他浏览器,只需将文本文件加载到pre
标记中即可):
$(“#数据预”)加载(“lorem.txt”,函数(响应、状态、xhr){
如果(状态!=“错误”){
if($.browser.msie)&&(jQuery.browser.version==“8.0”)//检查浏览器是否为Internet Explorer
$(“#data pre”).html(response.replace(//\r\n/g,“
”);//将新行替换为
标记
}
});
HTML:
尝试将其加载到
元素中,或。相关的:
$("#data pre").load("lorem.txt", function(response, status, xhr) {
if (status != "error") {
if (($.browser.msie)&&(jQuery.browser.version=="8.0"))//check if browser is Internet Explorer
$("#data pre").html(response.replace(/\r\n/g, "<br>"));//replace new lines with <br> tag
}
});
<div id="data">
<pre></pre>
</div>