换行符( )没有按预期工作
Line Break ( ) is not working as expected
我试图分割值(每个项目由管道符号分隔)并在新行中显示每个值。我试图在每个值之间添加'/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> Download 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
我想知道为什么位置的值不显示在新行…
使用<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> Download 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>
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>";
}
在HTML中有多个空白符号,大多数'#
符号被忽略(如't
, 'n
…)。
HTML中的新行符号为<br/>
。
也不是/n
,而是'n
或'r'n
或'r
(取决于系统)。
相关文章:
- 如何在映射数组中添加换行符
- JQuery:向多个匹配结果添加换行符的最简单方法
- 在文本区域中使用jQuery.text()保持换行符
- javascript替换换行符和特殊字符
- 换行符拆分返回“”;未定义”;
- 如何使用web浏览器在NoSQL数据中呈现换行符
- 换行符插入jquery选择器
- jquery使用val()复制值似乎可以删除换行符
- 文本区域-获取每一行,找到换行符
- 处理字段中带有换行符的csv文件-node.js
- 使用jquery将单个换行符替换为span元素
- javascript/jQuery:在title中添加换行符——在IE9中有效,但在IE8中无效
- 咖啡脚本意外换行符
- 在文本区域中保留换行符.value
- 捕捉文本中的换行符
- 在字符串中遇到换行符时,Javascript无法在Visual Force页面上工作
- Jquery换行符替换为br标签不工作
- 使用fromCharCode的HTML换行符不工作
- 换行符( )没有按预期工作
- 为什么我的JS不工作时设置为'onLoad'但当设置为'无换行符'时有效