如何在javascript中将html粗体标记添加到嵌入的html代码中?

时间:2016-01-04 19:29:04

标签: javascript html

我无法在代码中的几个项目上添加粗体标记。它会加粗整个单词,包括添加的变量。 (我不希望变量加粗)。谢谢!

"<p>" + " Given the severity of the change listed below, you are being asked to review and either approve or reject this change. Submitted by: "  + row.requester
+ "<p>" + "<b>Description: </b>" + row.commentsAndGeneralDescriptionOfTheChangeBeingImplementedAndAnySpecialInstructions
+ "<p>" + "<b>Report Id: </b>" + row.rowNumber

以上是上述代码段的其他一些背景信息:

function sendReportToManager(row) { 
    var message = "<HTML><BODY>" + "<p>" +" Given the severity of the change listed below, you are being asked to review and either approve or reject this change. Submitted by: " + row.requester + 
    "<p>" + "<b>Description: </b>" + row.commentsAndGeneralDescriptionOfTheChangeBeingImplementedAndAnySpecialInstructions + 
    "<p>" + "<b>Report Id: </b> " + row.rowNumber + 
    '<p><b>Please approve or reject <A HREF="' + APPROVAL_FORM_URL + '">Change Request</A> #' + row.rowNumber + "</HTML></BODY>";
     MailApp.sendEmail(row.changeManagerApproval, "Change Management Approval - REQUEST", "", {noReply: true, htmlBody: message}); 
} 

2 个答案:

答案 0 :(得分:0)

CodePen

你的例子有效。您所拥有的问题(为什么粗体不会终止)无法通过您提供的信息来确定。如果您的变量(例如row.requester)有一个开放的<strong><b>,它将适用于HTML,直到遇到其结束标记。

如果您的邮件客户端接受它,更好的方法是使用CSS和更好的HTML标记。例如:

"<p>" 
   + "Given the severity of the change listed below, you are being asked to review and either approve or reject this change. "
   + '<span class="label">Submitted by: </span><span class="value">'  
   + row.requester
+ "<p>" 
   + '<span class="label">Description: </span><span class="value">' 
   + row.commentsAndGeneralDescriptionOfTheChangeBeingImplementedAndAnySpecialInstructions
   + "</span>"
+ "<p>" 
   + '<span class="label">Report Id: </span><span class="value">' 
   + row.rowNumber
   + "</span>"

然后添加一些CSS:

.label { font-weight: bold; }
.value { font-weight: normal; }

注意: 在字符串中嵌入HTML标记还有其他问题,正如您上面所做的那样 - 此答案并未解决这些问题。相反,这个答案应该被用作解决问题中问题的第一步。

答案 1 :(得分:-3)

看起来你有一些你不需要的额外东西。试试这个:

"<strong>Given the severity of the change listed below, you are being asked to review and either approve or reject this change.</strong>" + \n  +
"<strong>Submitted by:</strong> " + row.requester +  \n +
"<strong>Description: </strong> "+ row.commentsAndGeneralDescriptionOfTheChangeBeingImplementedAndAnySpecialInstructions +  \n +
"<strong>Report Id:</strong> " + row.rowNumber