gpt4 book ai didi

javascript - AJAX 和 jQuery 的分离

转载 作者:行者123 更新时间:2023-12-02 17:41:35 24 4
gpt4 key购买 nike

我确定之前已经有人问过这个问题,所以如果有重复,我很抱歉。我正在使用 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.

感谢您的帮助。

最佳答案

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

关于javascript - AJAX 和 jQuery 的分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com