css - 没有值的自定义 css 属性
问题描述
我想构建一个禁用元素的自定义 css 属性(而不是使用disabled
关键字)
所以,我写了下面的,但它没有用
<style>
.admin {
color: green;
enable:
}
.user {
color: red;
--btn-disable: 0;
}
@property --btn-disable
{
syntax: '<number>';
cursor: not-allowed;
pointer-events: none;
/*Button disabled - CSS color class*/
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
.btn-disabled
{
cursor: not-allowed;
pointer-events: none;
/*Button disabled - CSS color class*/
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
</style>
<h1>{{.PageTitle}}</h1>
<ul>
{{range .Todos}}
<input class={{.Classes}}>{{.Title}}</li>
{{end}}
</ul>
上面的模板将给出如下内容:
<html><head><style>
.admin {
color: green;
enable:
}
.user {
color: red;
--btn-disable: 0;
}
@property --btn-disable
{
syntax: '<number>';
cursor: not-allowed;
pointer-events: none;
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
.btn-disabled
{
cursor: not-allowed;
pointer-events: none;
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
</style>
</head><body><h1>My TODO list</h1>
<ul>
<input class="admin btn-disabled">Task 1
<input class="user">Task 2
<input class="admin user">Task 3
</ul>
</body></html>
第一个输入的样式使用.btn-disabled
它工作正常,第二个输入的样式使用--btn-disabled
它不能正常工作。最后一个元素同时使用两种方式设置样式,但效果不佳,很可能--btn-disabled
是最后一个实现的样式。
解决方案
我能够使用 解决它[data-any="xxx"]
,如下所示:
<html><head><style>
[data-authorized="no"] {
cursor: not-allowed;
pointer-events: none;
color: #c0c0c0;
background-color: rgb(229, 229, 229) !important;
}
</style>
</head><body><h1>My TODO list</h1>
<ul>
<input type="text" data-authorized="yes">Task 1
<input type="text" data-permissions="user" data-authorized="no">Task 2
<input type="text" data-authorized="yes">Task 3
</ul>
<script>
var flags = ["admin", "super user"]
var elements = document.querySelectorAll("input");
elements.forEach((element, index, array) => {
if(element.hasAttribute("data-permissions")){
console.log(element.dataset.permissions)
var perm = element.dataset.permissions.split(" ");
var found = false;
for (var i = 0; i < perm.length; i++) {
if (flags.indexOf(perm[i]) > -1) {
element.dataset.authorized = "yes"
element.removeAttribute("data-permissions")
break;
}
}
}
});
</script>
</body></html>
考虑到对这个问题的宝贵意见,我必须避免在客户端使用它,现在开始寻找如何在服务器端使用它。
推荐阅读
- python - 删除列表的第一个重复元素
- wordpress - Divi 模块插件开发阶段后可能的下一步
- dll - Compaq Visual Fortran 6.6、动态链接库 (DLL) 和模块
- apache-spark - 在单台机器上运行的 Spark 机器学习:它是分布式的还是非分布式的?
- xpages - 后端文档组合框字段值未显示在网页上
- javascript - 如何检查弹簧表单的字段是否被禁用?
- go - 多个 go 实例共享一个公共通道
- mysql - 如何减少执行选择查询时间
- javascript - 如何使用javascript在选择标签中添加年份
- c# - 如何在python35中使用C# Dll