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

How to add html bold tag to an embedded html code in javascript?

本文关键字:html 添加 代码 javascript      更新时间:2023-09-26

我在代码中的几个项目上添加粗体标记时遇到了问题。它将用粗体显示整个单词,包括添加的变量。(我不希望变量用粗体显示)。谢谢

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

CodePen

你的例子很管用。你的问题(为什么粗体字没有终止)不能通过你提供的信息来确定。如果您的变量(例如row.requester)有一个打开的<strong><b>,它将应用于HTML,直到遇到它的结束标记。

如果您的邮件客户端接受CSS和更好的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标记还有其他问题,正如您上面所做的那样-这个答案并不能解决这些问题。相反,这个答案应该作为解决所问问题的第一步

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

"<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