【问题标题】:error when using else in EJS. compilation error [duplicate]在 EJS 中使用 else 时出错。编译错误[重复]
【发布时间】:2021-07-13 08:22:17
【问题描述】:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>To Do List</title>
</head>
<body>
<%if(kindOfDay==="sunday"||kindOfDay==="saturday")%>
<% { %>
<h1 style="color:pink;">Today is <%=kindOfDay%></h1>
<% } %>
<% else{%>
<h1 style="color:blue">Today is <%=kindOfDay%></h1>
<% }%>
</body>
</html>
错误:编译 ejs 时 C:\Users\Dell\desktop\webd\todolist-v1\views\list.ejs 中出现意外的令牌 'else'
【问题讨论】:
标签:
html
node.js
express
compiler-errors
ejs
【解决方案1】:
不要拆分} else
这样会更好
<% if(kindOfDay==="sunday"||kindOfDay==="saturday") { %>
<h1 style="color:pink;">Today is <%=kindOfDay%></h1>
<% } else { %>
<h1 style="color:blue">Today is <%=kindOfDay%></h1>
<% } %>
或者
<h1 style="color: <%= kindOfDay==="sunday"||kindOfDay==="saturday ? "pink" : "blue" %>">Today is <%=kindOfDay%></h1>
或者
<h1 class="<%=kindOfDay%>">Today is <%=kindOfDay%></h1>
使用css
h1 { color: blue }
h1.sunday, h1.saturday { color: pink }
h1 { color: blue }
h1.Sunday, h1.Saturday { color: pink }
<h1 class="Sunday">Today is Sunday</h1>
<h1>Tomorrow is Monday</h1>