css - 动态设置 html 代码中的选择器样式
问题描述
我有三个按钮。悬停时它们有动画。我用 ejs 渲染引擎加载我的文件。我想为三个按钮设置不同的动画颜色,并在发送 html 页面时动态设置这些颜色。
我试过这样的事情:
<button class="button button--style" onclick="window.location.href='/location'" style=":after {background: <%= color %>;}">Button 1</button>
所有的动画都是通过 tbutton-style
定义的,我还在那里定义了 after 样式:
.button--style::after {
background: #f00;
}
我怎么能做到这一点?
解决方案
您必须改用类并在后面的代码中设置您的类。如前所述,您不能在样式标签中使用伪类;
<button class="button button--style" onclick="window.location.href='/location'" class="<%= class1variable %>">Button 1</button>
class1::after {
background-color: #F0F0F0;
}
class2::after {
background-color: #BLAH;
}
推荐阅读
- wordpress - 像亚马逊一样设置“包括缺货”过滤器
- servicenow - 如何使用 Rest API 获取具有字段属性的特定视图/布局元字段
- image - 如何在 Wear OS 中替换表盘的手矢量?
- reactjs - 如何以开玩笑的 spyOn 方法为 GET 调用传递模拟数据
- r - 如何使列的某些元素彼此相等?
- c++ - 如何使用 ODBC API 指定 TABLOCK
- sql - Pyspark spark.sql 基于主键和条件的合并
- database - 主数据库服务器故障后 ArangoDB 丢失数据
- android - 为什么片段被销毁时片段仍然保留它的内存地址
- html - 使用 css 撤消滚动条样式