lrony* 2014-03-04 01:36 采纳率: 0%
浏览 33

AJAX和jQuery拆分

I'm sure this question has been asked before, so I'm sorry if it's a duplicate. I'm pulling in data from a text document using AJAX. How would I separate the paragraphs within the AJAX call so that the paragraphs are split on a paragraph break. I'm showing a change in the html document, but I'm not receiving the results I want. The paragraphs are not splitting up, instead, the paragraph break is simply being removed. FYI, I'm not very experienced with AJAX.

JAVASCRIPT

$(document).ready(function(){
    $.ajax({
        type: "GET",
        dataType: "text",
        url: "text.txt",
        success: function(txt){
            var str = (txt);
            str = (str.split("
"));
            $("#test").append(str);
        }
    });
})

TEXT

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.

This is also the desired output I want, however, This is what I'm getting. I've capitalized where the paragraphs are supposed to split.

OUTPUT

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.

Thanks for the help.

  • 写回答

2条回答 默认 最新

  • weixin_33724046 2014-03-04 01:44
    关注
    var str = text.split('
    
    ').map(function(p) { return '<p>' + p + '</p>'; }).join('
    
    ')
    

    or, if you need it to work in obsolete browsers,

    var str = $.map(text.split('
    
    '), function(p) { return '<p>' + p + '</p>'; }).join('
    
    ')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题