2015-09-07 164 views
2

我正在一个应用程序中生成一个包含一个mailbody的字符串,并放入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现在我要做我必须表明它在引导带,会给喜欢看的确切浏览器和感觉。

我得到的字符串像

   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) 
}); 

jQuery的响应,在返回完整的HTML警报,我怎么能得到这样的感觉浏览器?

回答

1

我建议你创建自定义提醒框并将jQuery嵌入到它中。

例如让一个div:

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

而不是地方的html进去:

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

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

编辑:

ü pdated小提琴与bootbox(引导警报)一起工作。

https://jsfiddle.net/nszag875/3

+0

@your解决方案是不错,但我要像一个弹出我可以使用Bootbox吗? – lucifer

+0

或者你可以参考一些自定义警告框? – lucifer

+0

是的,你可以使用bootbox,这里是更新的小提琴: https://jsfiddle.net/nszag875/3/ – Shark4109

0

您不能使用JavaScript内置的alert()方法来完成这项工作,它不能显示比字符串更多的东西。为了您的目的,您可以使用一些JavaScript库或jQuery插件,如使用jQuery UI的jQuery Dialog(http://jqueryui.com/dialog)。它会要求您制作一个<div>并将您的字符串附加到该div。就像这样:

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

正如你所说,你想使你的弹出消息,对于您可以使用动画与模态对话框。

0

在你应该使用jQuery的功能,您的字符串数据作为DOM元素追加到DOM树 例如我的意见:

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