【问题标题】:React: Changing the background color of TreeSelect (antD component)React:更改 TreeSelect 的背景颜色(antD 组件)
【发布时间】:2020-08-09 20:11:04
【问题描述】:

我正在尝试更改 antD 的 TreeSelect 组件的背景颜色

尝试了这些方法:

 // const bgColor = element.Style !== undefined && element.Style === Constants.StyleModes.MAJOR ?
                    //     "#CFECF5" : "white" //TODO - didn't override the ant-select-selector

                    // const listStyle = element.Style !== undefined && element.Style === Constants.StyleModes.MAJOR ?
                    //     "small-font styled-element" : "small-font"
                    elements.push(
                        <td className="view-cell" align="left">
                            {element.Name}
                            <br/>
                            <TreeSelect
                                className="small-font"
                                style={{ width: '100%', fontSize: 'small' /*, backgroundColor: bgColor*/}}

但它没有生效; 如果我将“!important”添加到bgColor const,则在检查元素时样式不会显示在元素中,但是如果我删除了其中显示的重要但未生效以导致antD样式“ant-select-selector”更强并且覆盖它:

如果我将此样式添加到我的 css 中,它将影响所有列表,我只想根据某些逻辑设置特定的样式:

 .ant-select-selector {
    background-color: #CFECF5 !important;
}

【问题讨论】:

    标签: javascript css reactjs antd


    【解决方案1】:

    试试这个(你可以在 DevTools 中看到正确的选择器):

    .treeSelect.ant-select-single:not(.ant-select-customize-input)
      .ant-select-selector {
      background-color: aliceblue;
    }
    
    <TreeSelect className="treeSelect"/>
    

    【讨论】:

    • 谢谢丹尼斯。它影响了所有列表的样式,而不是我感兴趣的唯一一个!
    • 不知道你的意思,那是根据你的形象的价值。
    • 我的意思是我的表单中有不止一个列表,我不想影响所有的列表......只有一些。你给的风格正在影响他们所有人
    • 为什么要这样?它只影响带有className="treeSelect" 的那些,请参阅更新的沙箱,它显示没有这种效果。
    • 忽略它,我的错(忘记删除旧样式)。非常感谢
    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2013-08-08
    相关资源
    最近更新 更多