Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX和jQuery的拆分_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Javascript AJAX和jQuery的拆分

Javascript AJAX和jQuery的拆分,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我肯定这个问题以前有人问过,所以如果是重复的话,我很抱歉。我正在使用AJAX从文本文档中提取数据。我如何在AJAX调用中分离段落,以便在段落中断时分割段落。我正在显示html文档中的更改,但没有收到我想要的结果。段落没有分开,相反,段落分隔符只是被删除。仅供参考,我对AJAX不是很有经验 JAVASCRIPT $(document).ready(function(){ $.ajax({ type: "GET", dataType: "text",

我肯定这个问题以前有人问过,所以如果是重复的话,我很抱歉。我正在使用AJAX从文本文档中提取数据。我如何在AJAX调用中分离段落,以便在段落中断时分割段落。我正在显示html文档中的更改,但没有收到我想要的结果。段落没有分开,相反,段落分隔符只是被删除。仅供参考,我对AJAX不是很有经验

JAVASCRIPT

$(document).ready(function(){
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "text.txt",
        success: function(txt){
            var str = (txt);
            str = (str.split("\n"));
            $("#test").append(str);
        }
    });
})
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never
does, the truth is, break-ins happen. The best way to deal with this dangerous
situation is to install a security system that will help prevent thieves from
entering your home and causing harm to your family or taking your valuables.

Our monitoring station operates 24 hours a day 7 days a week and 365 days a year.  
We   offer security that you can rely on, which means that you can be assured that
your home is being watched by our professionals. Our monitoring service is
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never 
does, the truth is, break-ins happen. The best way to deal with this dangerous 
situation is to install a security system that will help prevent thieves from 
entering your home and causing harm to your family or taking your valuables.OUR 
monitoring station operates 24 hours a day 7 days a week and 365 days a year. 
We offer security that you can rely on, which means that you can be assured that 
your home is being watched by our professionals. Our monitoring service is 
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
文本

$(document).ready(function(){
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "text.txt",
        success: function(txt){
            var str = (txt);
            str = (str.split("\n"));
            $("#test").append(str);
        }
    });
})
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never
does, the truth is, break-ins happen. The best way to deal with this dangerous
situation is to install a security system that will help prevent thieves from
entering your home and causing harm to your family or taking your valuables.

Our monitoring station operates 24 hours a day 7 days a week and 365 days a year.  
We   offer security that you can rely on, which means that you can be assured that
your home is being watched by our professionals. Our monitoring service is
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never 
does, the truth is, break-ins happen. The best way to deal with this dangerous 
situation is to install a security system that will help prevent thieves from 
entering your home and causing harm to your family or taking your valuables.OUR 
monitoring station operates 24 hours a day 7 days a week and 365 days a year. 
We offer security that you can rely on, which means that you can be assured that 
your home is being watched by our professionals. Our monitoring service is 
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
这也是我想要的输出,然而,这是我得到的。我已经把段落应该分开的地方大写了

输出

$(document).ready(function(){
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "text.txt",
        success: function(txt){
            var str = (txt);
            str = (str.split("\n"));
            $("#test").append(str);
        }
    });
})
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never
does, the truth is, break-ins happen. The best way to deal with this dangerous
situation is to install a security system that will help prevent thieves from
entering your home and causing harm to your family or taking your valuables.

Our monitoring station operates 24 hours a day 7 days a week and 365 days a year.  
We   offer security that you can rely on, which means that you can be assured that
your home is being watched by our professionals. Our monitoring service is
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
With crime at an all time high, the need for home security has become an even
greater necessity. Although it may not happen to you, and we all hope it never 
does, the truth is, break-ins happen. The best way to deal with this dangerous 
situation is to install a security system that will help prevent thieves from 
entering your home and causing harm to your family or taking your valuables.OUR 
monitoring station operates 24 hours a day 7 days a week and 365 days a year. 
We offer security that you can rely on, which means that you can be assured that 
your home is being watched by our professionals. Our monitoring service is 
offered at a low price, you shouldn't look at it as a cost, but more of an 
investment.
谢谢您的帮助。

var str=text.split('\n\n').map(函数(p){return''+p+'

';})。join('\n\n'))
var str = text.split('\n\n').map(function(p) { return '<p>' + p + '</p>'; }).join('\n\n')
或者,如果您需要它在过时的浏览器中工作

var str = $.map(text.split('\n\n'), function(p) { return '<p>' + p + '</p>'; }).join('\n\n')
var str=$.map(text.split('\n\n'),函数(p){return''+p+'

';})。join('\n\n'))
只需将拆分的代码替换为replace,这样换行符就会被HTML等价物替换
,如下所示:

function(txt){
    stri = txt.replace(/\n/g, "<br/>");
    $("#test").append(stri);
}
函数(txt){
stri=txt.replace(/\n/g,“
”); $(“#测试”)。追加(stri); }
替换使用正则表达式/\n/检测换行符\n


注意:g后缀表示全局,例如处理所有出现的\n

如何将它们分开?你认为stri是什么
console.log()
it!哎呀,stri应该是str,我的错。我修好了。