javascript - 将\n 换行符转换为 HTML <BR> 换行符

标签 javascript jquery

我有一个简单的想法来转换我的 \n我的 ajax JSON 调用中的换行符 <br>这样它就能正确显示我的数据。

我尝试了许多不同的方法来执行转换,但我不断收到错误消息:

Uncaught TypeError: data.replace is not a function

The issue seems to be the way I'm making this call:

var incident = incident.replace(/\n/g, "<br />");

从我在论坛上看到的来看,这就是我们应该使用.replace的方式但我不明白为什么它不喜欢它。

我也尝试这样做:

"<td colspan='3'>"+incident.Impact.replace(/\n/g, "<br />")+"</td>"+

这有效,但如果该字段为空,则会返回错误,指出无法读取 .replace未定义的。有人有什么想法吗?

完整代码:

$.ajax({
    url: this.basePath() + '/GDI_PROD_Incidents?$filter=ÉtatValue%20ne%20%27Fermé%27&$orderby=PrioritéValue desc',
    dataType: 'json',
    cache: false,
    success: function (data) {

        $.each(data.d.results, function (index, incident) {
        var incident = incident.replace(/\n/g, "<br />");

        $('body').append(

          "<table border='1' width='800' >"+
          "<tr  bgcolor='#9aff99' align='center'>"+
          "<td width='800px' colspan='4'><strong>Transfert de connaissances</strong></td>"+
          "</tr>" +
          "<tr bgcolor='#fff'>"+
          "<td width='165'>Billet:</td>"+
          "<td>"+incident.Incident+"</td>"+
          "<td width='165'>Priorité:</td>"+
          "<td>"+incident.PrioritéValue+"</td>"+
          "</tr>"+
          "<tr bgcolor='#fff'>"+
          "<td width='165'>Description:</td>"+
           "<td colspan='3'>"+incident.Description+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td colspan='4' width='800px' bgcolor='#9aff99' align='center'>Détails</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Impact:</td>"+
           "<td colspan='3'>"+incident.Impact+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Dépannage</td>"+
           "<td colspan='3'>"+incident.Dépanage+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td colspan='4' width='800px' bgcolor='#9aff99' align='center'>Suivi</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Prochain Suivi:</td>"+
           "<td colspan='3'>"+incident.Suivi+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Ressource:</td>"+
           "<td colspan='3'>"+incident.Ressources+"</td>"+
           "</tr>"+
           "<tr bgcolor='#fff'>"+
           "<td width='165'>Prime:</td>"+
           "<td colspan='3'>"+incident.ResponsableValue+"</td>"+
           "</tr>"+
           "</table>"+
           "<br><br>");

        })

    }
});

最佳答案

您正在尝试对对象使用字符串方法,而不是对该对象内的属性值使用字符串方法。

没有Object.replace(),因此会抛出错误

同样undefined.replace()也会导致错误

大概您只需要修复一些属性值,但首先有必要在使用 replace() 之前确保这些值已定义并且是字符串

尝试

function convertBreak(str){
   if(!str){
     return ''; // don't want `undefined` printing into page
   }
   // if it's something other than string, return it as is
   if( typeof str !=== 'string'){
      return str;
   }else{
     return str.replace(/\n/g, "<br />")
    }
}

incident.Description = convertBreak(incident.Description);

关于javascript - 将\n 换行符转换为 HTML <BR> 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880008/

相关文章:

javascript - 什么时候不触发 textarea 更改事件?

jquery - 如何在点击事件上调用 jQuery AJAX?

javascript - Jquery .ajax() 局部变量不能分配给全局

javascript - 使用 jQuery data() 方法存储函数

javascript - 为了选择或聚焦对象,如何使右键单击行为与左键单击相同

javascript - 更改数据表的页脚

javascript - 传递空格键的当前值 :this as argument to new helper in meteor/blaze

asp.net - 如何从 Jquery 调用 ActionResult

javascript - 为什么我的 jQuery UI CSS 在 JSFiddle 中不起作用?

javascript - 我的 Angular 应用程序的 Pdf 查看器