jQuery的.html()如何处理JavaScript字符串?
我有express应用程序和EJS作为视图引擎。 我试图用两种不同的方式将一些html设置为div:jQuery的.html()如何处理JavaScript字符串?,jquery,html,ejs,Jquery,Html,Ejs,我有express应用程序和EJS作为视图引擎。 我试图用两种不同的方式将一些html设置为div: const attachmentFileNames = '<%= eco.attachmentFileName %>'; if (attachmentFileNames) { $("#attachmentFileNameList").html(attachmentFileNames.replace(',', '<br/>')); } const attachme
const attachmentFileNames = '<%= eco.attachmentFileName %>';
if (attachmentFileNames) {
$("#attachmentFileNameList").html(attachmentFileNames.replace(',', '<br/>'));
}
const attachmentFileNames='';
if(附件文件名){
$(“#attachmentFileNameList”).html(attachmentFileNames.replace(',','
');
}
及
const attachmentfilename=”“;
if(附件文件名){
$(“#attachmentFileNameList”).html(attachmentFileNames);
}
问题是,第一个和平的代码按预期工作(“”被视为行终止符),但第二个和平的代码只是将所有数据设置为文本(“
”显示为字符串)
有人能解释一下吗?这不是jQuery的
html
函数,而是EJS,它自动转义由
表达式生成的文本
在第二个示例中,如果在调试器中查看attachmentFileNames
的值,您可能会看到br/>
(或br/
)而不是
。当您将“br/>”
(或“br/”
)用作HTML时,结果是字符
:
const attachmentFileNames1='one,two';
如果(附件FileNames1){
$(“#attachmentFileNameList1”).html(attachmentFileNames1.replace(',','
');
}
const attachmentFileNames2='onebr/>two';
如果(附件文件名2){
$(“#attachmentFileNameList2”).html(attachmentFileNames2);
}
const attachmentFileNames = "<%= eco.attachmentFileName ? eco.attachmentFileName.replace(',', '<br/>') : '' %>";
if (attachmentFileNames) {
$("#attachmentFileNameList").html(attachmentFileNames);
}