You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在EJS中将标题颜色设置为数据库存储的色值

问题原因

当前写法是将颜色赋值逻辑作为文本直接输出到了<p>标签内部,所以页面只会显示hex色值,不会生效为字体颜色。你需要把数据库取出的色值注入到元素style属性的color配置项中。

正确实现代码

<div class="inner">
  <p style="color: <%= notes.color %>"><%= notes.heading %></p>
</div>

注意事项

  • 要确保数据库存储的notes.color是合法颜色格式,比如带#前缀的hex值、rgb值或者标准颜色关键字,否则颜色不会生效
  • 如果需要保留notes.heading.color的赋值逻辑,可以单独写在模板逻辑段中,不要放到内容输出的位置:
<% notes.heading.color = notes.color %>
<div class="inner">
  <p style="color: <%= notes.color %>"><%= notes.heading %></p>
</div>

内容的提问来源于stack exchange,提问作者Fatima sami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 12:45:04