繁体   English   中英

如何将字符串转换为html

[英]How to convert the String to html

我在一个应用程序中工作,我生成了一个包含邮件正文的字符串,放置 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)

示例小提琴在这里: https : //jsfiddle.net/nszag875/

编辑:

更新了小提琴以使用引导箱(引导程序警报)。

https://jsfiddle.net/nszag875/3

您不能使用 JavaScript 的内置alert()方法来完成这项工作,它只能显示字符串。 出于您的目的,您可以使用一些 JavaScript 库或 jQuery 插件,例如使用 jQuery UI 的 jQuery Dialog ( http://jqueryui.com/dialog )。 它将要求您创建一个<div>并将您的字符串附加到该 div。 像这样 :

$('#divYouCreated').html('Your string here');

正如你所说,你想让你的消息弹出,为此你可以使用带有模态框的动画。

在我看来,您应该使用 jquery 功能将字符串数据作为 DOM 元素附加到 DOM 树,例如:

    var template = '<p> simple paragraph</p>'; // your string data
    var $template = $(template);
    $('#some-id').append($tamplate);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM