如何在javascript中为嵌入的html代码添加html粗体标记
How to add html bold tag to an embedded html code in javascript?
我在代码中的几个项目上添加粗体标记时遇到了问题。它将用粗体显示整个单词,包括添加的变量。(我不希望变量用粗体显示)。谢谢
"<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
相关文章:
- 将 HTML 添加到 Javascript IF 函数
- 如何使用 javascript 向我的 html 添加内容而不擦除整个页面
- 如何更新浏览器'使用Ajax将html添加到页面后的缓存
- 有没有一种方法可以用javascript将原始html添加为新的表行
- Javascript和HTML - 添加一个下拉菜单,我在这里做错了什么
- 在我的情况下,如何将html添加到Angular中
- Facebook的“赞”按钮如何具有跨域访问权限,可以将HTML添加到父窗口
- 使用 JavaScript 向 HTML 添加数据、类和样式
- HTML - 添加和删除收藏夹
- 元素上的 HTML 添加属性
- ng-bind-html 添加拥有 CSS 类“ng-binding”,它破坏了所有嵌套的 css,如何摆脱它
- 将.html添加到字符串 Javascript
- 在Angular对象中循环时向HTML添加id
- 在将html添加到文档中之前,如何修改ajax调用中的html
- 将HTML添加到JQuery同位素插件/插入项第2部分
- 将HTML添加到JQuery同位素加载项/插入项
- 将HTML添加到iframe会导致页面重新加载
- JQuery:将HTML添加到父分区作为AJAX的结果
- Backbone Rails:如何在application.HTML.erb中为HTML添加javascript
- 使用jQuery向HTML添加Maxlength属性