2015-09-07 202 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);