javascript - 如何将字符串转换为html

标签 javascript java jquery html json

我在一个应用程序中工作,我在其中生成了一个包含邮件正文的字符串,采用 HTML 格式。现在我想在一个看起来完全像浏览器输出的警报中展示它。我在这里给出我的代码

mailBody = "<html>"
                                  +"<center> "      
                                  +"<body style='background-color: #e5e4e2;'> " 
                                  +"<table id='mainTable' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'>" 
                                  +"<tr> " 
                                  +"<td> "      
                                  +"<table style='width: 100%;background-color: #2B547E; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'> " 
                                  +"<tr> " 
                                  +"<td width='50%'><img src=\"cid:image\" height='50' width='150' style='padding-left: 10px;'></td> " 
                                  +"<td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td>" 
                                  +"</tr> " 
                                  +"</table> "  
                                  +"</td> " 
                                  +"</tr>" 
                                  +"</table> " 
                                  +"<table id='mainTable1' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> " 
                                  +"<tr> "
                                  +"<td> "
                                  +"<table style='width: 100%; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'>" 
                                  +"<tr> " 

                                  +"<td width='50%' style='color:red;font-size:21px;'>Congratulations, "+rsServeResource.getString(1)+" </td> " 

                                  +"<td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td> " 
                                  +"</tr> " 
                                  +"<tr> "
                                  +"<td style='font-size:18px;width:97%;font-style:italic'> "
                                  +emailText+" on "+interviewPhoneorOnsiteDate+" For " +rsServeResource.getString(2)+"  in   "+rsServeResource.getString(3)+" , "
                                  + "  You will be called at your mobile number "+rsServeResource.getString(4)    
                                  +"</td> "
                                  +"</tr> "
                                  +"</table> " 

                                  +"</td> "
                                  +"</tr> "
                                  +"</table> "
                                  +html
                                  +"<table id='mainTable2' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto; margin-top:12px;'>" 
                                  +"<tr> "
                                  +"<td style='font-size:18px; color:#2B547E;word-wrap: break-word;font-style: italic'> "

                                  +message

                                  +"</td> " 
                                  +"</tr> "
                                  +"</table> "
                                  +"<table  style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> "
                                  +"<tr> "
                                  +"<td style='font-size:18px;color:#2B547E;word-wrap: break-word;font-style: italic;font-weight: bold'> "
                                  +"Best Regards,"
                                  +"</td>"
                                  +"</tr>"
                                  +"<tr>"
                                  +"<td style='font-size:24px;color:#2B547E;word-wrap: break-word;font-style: italic;font-weight: bold'> "
                                  +consultantName
                                  +"</td>"
                                  +"</tr> " 
                                  +"<tr>"
                                  +"<td style='font-size:18px;color:#2B547E;word-wrap: break-word;font-style: italic; font-weight: bold'> "
                                  +"Client Service Manager"
                                  +"</td>"
                                  +"</tr> "                               
                                  +"</td>"
                                  +"</tr>"
                                  +"</table>"
                                  +"</body> " 
                                  +"</center> " 
                                  +"</html> ";  

这就是我正在生成的字符串,现在我必须从 java 中显示它,我必须在引导带中显示它,这将提供准确的浏览器外观。

我在 jQuery 响应中获取字符串,例如

             searchResultArray.put(mailBody);
             jsonFeed.put("searchResultArray", searchResultArray);
             resourceResponse.setContentType("text/html");
             resourceResponse.setCharacterEncoding("UTF-8");
             resourceResponse.getWriter().write(jsonFeed.toString());

 jQuery.getJSON(url+"&candidateId=" +candidateId+"&jobId="+jobId+"&text1="+text1+"&text2="+text2+"&text3="+text3+"&interviewVenue="+interviewVenue+"&interviewCC="+interviewCC+"&start-date="+startDate+"&interviewBCC="+interviewBCC+"&interviewCC="+interviewCC+"&interviewType="+interviewType+"&type="+type, function(data)  {  
    alert(data.searchResultArray)
});

返回完整 HTML 时的提示,我怎样才能获得类似浏览器的感觉?

最佳答案

我建议您创建自定义警告框并将带有 jQ​​uery 的 html 嵌入其中。

例如制作一个div:

<div id="message"></div>

然后将 html 放入其中:

$("#message").html(mailBody)

示例 fiddle 在这里: https://jsfiddle.net/nszag875/

编辑:

更新了 fiddle 以使用 bootbox(引导警报)。

https://jsfiddle.net/nszag875/3

关于javascript - 如何将字符串转换为html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440264/

相关文章:

javascript - 如何获取 highcharts 中 yAxis 标签的最高值?

javascript - Jquery event.preventDefault

java - Bukkit 的无效 plugin.yml 异常

javascript - 从水平选项卡列表中激活部分可见选项卡并在单击它时显示完整选项卡

间隔执行后的 JavaScript 回调

javascript - 无法在 'postMessage' : https://www. youtube.com 上执行 'DOMWindow' !== http://localhost:9000

java - 在服务器硬件上播放音频

java - Spring @ModelAttribute 和翻译请求参数绑定(bind)名称

php - 何时使用动态内容以及何时切换到另一个页面?

html - 同一元素内的同一类