首页 > 解决方案 > 如何更改 Html.DisplayFor 布尔结果的颜色

问题描述

Html.DisplayFor 在布尔值上的呈现结果是一个禁用的复选框。我只是想改变它的颜色。我看过很多网站,包括这个网站,但围绕这个主题的大部分帮助都是针对那些想要将显示更改为两个文本值之间的人。

我创建了一个自定义 css 文件并在我的布局页面中引用它,但它没有做任何事情。

input[type="checkbox"].disabled {
   color: #ccc
}
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - ASI Dashboard</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
    <link rel="stylesheet" href="~/css/styles/custom.css"/>
    @RenderSection("Style",false) 
</head>

在我的剃须刀页面中声明了该部分。

@section style{
}

我可以在 chrome 检查器中看到 HTML 是这样编写的,可以 在我的页面上@Html.DisplayFor(model => model.xx.yy)返回。<input class="check-box" disabled="disabled" type="checkbox"/>类型匹配,为什么颜色仍然没有改变?我知道我一定错过了什么。我究竟做错了什么?

编辑:查看元素本身后,所有“颜色”元素都被删除,我可以看到我的自定义样式。除了我的,它似乎没有其他颜色参考,但复选框仍然是暗灰色而不是改变颜色。

element.style {
}
input[type="checkbox"] {
    color: #e83e8c;
    background-color: #6f42c1;
    border-color: #ff6a00;
}
input[type=checkbox], input[type=radio] {
    box-sizing: border-box;
    padding: 0;
}
button, input {
    overflow: visible;
}
button, input, optgroup, select, textarea {
    margin: 0;
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
}
*, ::after, ::before {
    box-sizing: border-box;
}
user agent stylesheet
input[type="checkbox" i]:disabled {
    <strike>background-color: initial;</strike>
}
user agent stylesheet
input:disabled {
    <strike>background-color: -internal-light-dark-color(rgba(239, 239, 239, 0.3), rgba(59, 59, 59, 0.3));
    border-color: rgba(118, 118, 118, 0.3);</strike>
}
user agent stylesheet
input[type="checkbox" i] {
    -webkit-appearance: checkbox;
    box-sizing: border-box;
    margin: 3px 3px 3px 4px;
}
user agent stylesheet
input:disabled {
    <strike>color: -internal-light-dark-color(rgb(84, 84, 84), rgb(170, 170, 170));</strike>
    cursor: default;
}
user agent stylesheet
input[type="checkbox" i] {
    <strike>background-color: initial;</strike>
    cursor: default;
    margin: 3px 0.5ex;
    padding: initial;
    border: initial;
}
user agent stylesheet
input {
    -webkit-writing-mode: horizontal-tb !important;
    text-rendering: auto;
    <strike>color: -internal-light-dark-color(black, white);</strike>
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    -webkit-appearance: textfield;
    <strike>background-color: -internal-light-dark-color(rgb(255, 255, 255), rgb(59, 59, 59));
    -webkit-rtl-ordering: logical;
    cursor: text;
    margin: 0em;
    font: 400 13.3333px Arial;
    padding: 1px 2px;
    border-width: 2px;
    border-style: inset;
    <strike>border-color: -internal-light-dark-color(rgb(118, 118, 118), rgb(195, 195, 195));</strike>
    border-image: initial;
}
body {
    margin: 0;
    font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    text-align: left;
    background-color: #fff;
}
:root {
    --blue: #007bff;
    --indigo: #6610f2;
    --purple: #6f42c1;
    --pink: #e83e8c;
    --red: #dc3545;
    --orange: #fd7e14;
    --yellow: #ffc107;
    --green: #28a745;
    --teal: #20c997;
    --cyan: #17a2b8;
    --white: #fff;
    --gray: #6c757d;
    --gray-dark: #343a40;
    --primary: #007bff;
    --secondary: #6c757d;
    --success: #28a745;
    --info: #17a2b8;
    --warning: #ffc107;
    --danger: #dc3545;
    --light: #f8f9fa;
    --dark: #343a40;
    --breakpoint-xs: 0;
    --breakpoint-sm: 576px;
    --breakpoint-md: 768px;
    --breakpoint-lg: 992px;
    --breakpoint-xl: 1200px;
    --font-family-sans-serif: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";
    --font-family-monospace: SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;
}
@media (min-width: 768px)
html {
    font-size: 16px;
}
html {
    font-size: 14px;
}
html {
    font-family: sans-serif;
    line-height: 1.15;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}
*, ::after, ::before {
    box-sizing: border-box;
}
*, ::after, ::before {
    box-sizing: border-box;
}

编辑:我已经包含了检查员的屏幕截图。可以正确看到属性,但它们不适用。 检查员截图

标签: c#csscheckboxbootstrap-4razor-pages

解决方案


您正在尝试使用 disabled 作为输入类型复选框的一个类。所以input[type="checkbox"].disabled当你有这样的 HTML 时有效:

<input type="checkbox" class="check-box disabled" />

但这里不是这样。您的 HTML 如下所示:

<input type="checkbox" class="check-box" disabled="disabled" />

您正在添加一个属性(这是 HTML 禁用的默认行为)。因此,您需要更改 CSS 样式是使用以下任一方法:

input[type="checkbox"][disabled] {
    // customize here
}

或者

input[type="checkbox"]:disabled {
    // customize here
}

注意:我自己从未检查过第二个,但尝试过第一个。有用!


推荐阅读