javascript - 换行符 (\n) 未按预期工作

标签 javascript jquery css twitter-bootstrap

<分区>

我正在尝试拆分值(每个元素由管道符号分隔)并在新行中显示每个值。我尝试在每个值之间添加“/n”,但以下代码未按预期工作。

在我的例子中,值在 Location 字段中。

示例:“位置 A|位置 B|位置 C|位置 D”

//this data comes from server via ajax response

var rows = [];

var row1 = {};
row1.Name = 'Test User A';
row1.Location = 'Location A|Location B|Location C|Location D';
row1.Status = 'Success';

rows.push(row1);


//this code runs after receving the ajax call

$.each(rows, function(i, row){
  
  $('#StatusDataBody').append(GetRowTemplate(row));
  
  });


function GetRowTemplate(row){
  
  return "<tr>" +
                "<td class='text-center col-md-2'>" + row.Name + "</td>" +
                "<td class='col-md-3'>" + row.Location.split('|').join(',\n ') + "</td>" +
                "<td class='col-md-2'>" + row.Status + "</td>" +              
              "</tr>"; 
  }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <table class="table table-condensed table-custom">
                    <caption class="title-color">
                        <strong>Status Report</strong> | 
                <button type="button" id="DownloadStatusReportBtn" class="btn btn-success btn-xs disabled" title="Click here to download details"><span class="glyphicon glyphicon-download-alt"></span>&nbspDownload Detail</button>
                    </caption>
                    <thead>
                        <tr>                            
                            <th class="text-center col-md-2">Name</th>
                            <th class="text-center col-md-3">Location</th>
                            <th class="text-center col-md-2">Status</th>          
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="7">
                                <div id="StatusDataContainer">
                                    <table class="table table-bordered table-condensed">
                                        <tbody id="StatusDataBody"></tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>

期望:

值应如下所示显示,

Location A,
Location B,
Location C,
Location D

我想知道为什么 location 中的值没有显示在新行中...欢迎任何建议。

最佳答案

使用 <br />而不是 \n

//this data comes from server via ajax response

var rows = [];

var row1 = {};
row1.Name = 'Test User A';
row1.Location = 'Location A|Location B|Location C|Location D';
row1.Status = 'Success';

rows.push(row1);


//this code runs after receving the ajax call

$.each(rows, function(i, row){
  
  $('#StatusDataBody').append(GetRowTemplate(row));
  
  });


function GetRowTemplate(row){
  
  return "<tr>" +
                "<td class='text-center col-md-2'>" + row.Name + "</td>" +
                "<td class='col-md-3'>" + row.Location.split('|').join('<br />') + "</td>" +
                "<td class='col-md-2'>" + row.Status + "</td>" +              
              "</tr>"; 
  }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <table class="table table-condensed table-custom">
                    <caption class="title-color">
                        <strong>Status Report</strong> | 
                <button type="button" id="DownloadStatusReportBtn" class="btn btn-success btn-xs disabled" title="Click here to download details"><span class="glyphicon glyphicon-download-alt"></span>&nbspDownload Detail</button>
                    </caption>
                    <thead>
                        <tr>                            
                            <th class="text-center col-md-2">Name</th>
                            <th class="text-center col-md-3">Location</th>
                            <th class="text-center col-md-2">Status</th>          
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="7">
                                <div id="StatusDataContainer">
                                    <table class="table table-bordered table-condensed">
                                        <tbody id="StatusDataBody"></tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>

关于javascript - 换行符 (\n) 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693138/

上一篇:javascript - 如何在javascript中向上键的文本字段中的两位数字后添加点?

下一篇:html - Chromium 计算表格宽度不正确?

相关文章:

javascript - 从子组件调用父组件的方法 - React Native

javascript - 在我的网站上显示来自远程服务器的 RSS 源?

JavaScript 和 CakePHP : how to get this simple script working?

javascript - 在这种情况下,是否有替代全局变量的良好做法?

jquery - 使用伪类选择第一个 CSS 元素

css - 从中心显示背景图像

javascript - 如何在 Spotfire 分析中链接 CSS 样式表?

javascript - JavaScript 中 like 语句的概念

jquery - 将属性 "type"从文本更改为密码

javascript - 如何在回发后保留在javascript中设置的值