在 EJS 中使用 else 时出错。编译错误
error when using else in EJS. compilation error
<!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'
不要拆分 } 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>
<!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'不要拆分 } 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>