【问题标题】:Creating dynamic paragraphs for strings为字符串创建动态段落
【发布时间】:2014-02-19 10:12:52
【问题描述】:

我在使用响应创建动态<p> 元素时遇到以下问题!响应是纯文本。我需要确定字符串是否包含换行符或段落分隔符。

示例字符串

All rates are inclusive of the applicable Service charge, VAT, NBT & TDL currently at the prevailing 10%, 12%, 2% & 1% respectively. In the event of a change in the Service charge, VAT, NBT & TDL or the introduction of additional taxes, the Contract Rates will be adjusted accordingly.

 All rates are inclusive of the applicable Service charge, VAT, NBT & TDL currently at the prevailing 10%, 12%, 2% & 1% respectively. In the event of a change in the Service charge, VAT, NBT & TDL or the introduction of additional taxes, the Contract Rates will be adjusted accordingly.

 All rates are inclusive of the applicable Service charge, VAT, NBT & TDL currently at the prevailing 10%, 12%, 2% & 1% respectively. In the event of a change in the Service charge, VAT, NBT & TDL or the introduction of additional taxes, the Contract Rates will be adjusted accordingly.

这个字符串是三个段落组合在一起!如何识别每个单独的段落以使用 javascript 为每个段落创建单独的 <p> 标签。

【问题讨论】:

    标签: javascript jquery string substring


    【解决方案1】:

    你真的应该发布一些代码,但是试试这个

    var paragraphed = "<p>" + originalString.split("\n").join("</p><p>") + "</p>";
    

    【讨论】:

      【解决方案2】:

      试试这个:

        var responsestr="<your response text here>";  /*assuming that variable responsestr holds the response*/
        var str=encodeURIComponent(responsestr);
        var pArray=str.split("%0D%0A");
        var result=[];
        for(var i=0;i<pArray.length;i++){
          result.push("<p>"+decodeURIComponent(pArray[i])+"</p>\r\n");
        }
        var finalstr=result.join("");
      

      【讨论】:

        【解决方案3】:

        怎么样:

        var s = "your string";
        var paragraphs = s.split("\n")
              .filter(function (item) { return item !== ""; })
              .map(function (item) { return "<p>" + item + "</p>"; });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-05
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-21
          相关资源
          最近更新 更多