【问题标题】:In CSS, selecting parent <label> for an <input> that is selected在 CSS 中,为选中的 <input> 选择父 <label>
【发布时间】:2018-11-02 21:20:51
【问题描述】:

编辑:显然不能使用 大括号或隐藏名称?...

我已经看到这个问题的一些变体,但是我发现没有一个适合我的特定问题,我认为这是一个简单的问题。我正在反应中创建以下单选按钮组:

    const myOptions = ["YTD", "Week", "Month", "Pre AS", "Post AS"]
    const myButtons =
        <form>
            <div className="radio-group">
                {myOptions.map((d, i) => {
                    return (
                        <label>
                            <input
                                type={"radio"}
                                value={myOptions[i]}
                                checked={timeframeNew === myOptions[i]}
                                onChange={this.handleTimeframeNewChange}
                            />
                            <span>{myOptions[i]}</span>
                        </label>
                    )
                })}
            </div>
        </form>;

这是我当前的 CSS,用于设置按钮的样式以使其看起来不错...

input[type=radio] { 
  position: absolute;
  visibility: hidden;
  display: none;
}

label {
  color: #333;
  background: #EEE;
  display: inline-block;
  cursor: pointer;
  font-weight: bold;
  padding: 5px 20px;
  border: 2px solid orange;
}

input[type=radio]:checked + label {
  color: red;
  background: #333;
}

label + input[type=radio] + label {
  border-left: solid 2px blue;
}

.radio-group {
  border: solid 2px green;
  display: inline-block;
  margin: 20px;
  border-radius: 10px;
  overflow: hidden;
}

很遗憾,CSS 没有按预期工作。特别是,以下选择 - input[type=radio]:checked + label 不起作用,因为在 input 之后没有 label .到目前为止,我能够成功让我的 react onChange 处理函数工作的唯一方法是将输入放在标签内,就像这样,然后在每个 .map 循环中返回 label

*由于返回需要是单个元素,如果我想从 label 中取出,我需要将它们都包含在 div 中或 span,由于某种原因,这样做会破坏我的 onChange 处理程序...

所以我的问题是,我如何在 CSS 中获取与点击的输入相对应的标签。我想在未单击/未单击时更改整个标签的颜色和背景,因此选择跨度无济于事(因为这只会更改文本颜色/背景,而不是整个标签。

提前致谢!!

【问题讨论】:

    标签: css reactjs


    【解决方案1】:

    CSS 可以选择子元素和兄弟元素,但不能选择父元素。我经常隐藏默认的单选按钮和复选框并创建自己的,如下所示:

    .button-group{
      font-size:0; /*Prevents a space from occuring between buttons*/
    }
    .button-group input{
      position:absolute; 
      visibility:hidden; /* display:none causes some browsers to ignore the input altogether */
    }
    .button-group input+span{
      display:inline-block;
      line-height:20px;
      font-size:1rem;
      vertical-align:top;
      padding:0 10px;
      color:#000;
      border-left:1px solid #a00;
    }
    .button-group label:first-child input+span{
      border-radius:10px 0 0 10px;
      border-left:0;
    }
    .button-group label:last-child input+span{
      border-radius:0 10px 10px 0;
    }
    .button-group input:not(:checked)+span{
      background-color:#faa;
    }
    .button-group input:not(:checked)+span:hover{
      background-color:#f66;
    }
    input[type=radio]:checked+span{
      background-color:#f33;
    }
    <div class="button-group">
      <label>
        <input type="radio" value="1" name="myfield" />
        <span>Option 1</span>
      </label>
    
      <label>
        <input type="radio" value="2" name="myfield" />
        <span>Option 2</span>
      </label>
    
      <label>
        <input type="radio" value="3" name="myfield" />
        <span>Option 3</span>
      </label>
    </div>

    【讨论】:

    • 我希望将按钮连接为“单选按钮组”,这是我想选择标签而不是跨度的部分原因。
    • 我不确定您所说的“单选按钮组”是什么意思。你能提供一个例子或截图吗?
    • 其实我收回了,我不介意没有连接按钮
    • 我已经编辑了我的帖子以创建我认为您所说的“按钮组”。
    • @RobertAKARobin 我能问你为什么不简单地在输入中使用display:none 吗? (而不是所有的position, visibility, width, etc...
    【解决方案2】:

    *由于返回需要是单个元素,如果我想从标签中取出,我需要将它们都包含在 div 或 span 中,并且由于某种原因这样做会破坏我的 onChange 处理程序...

    您可以使用&lt;React.Fragment&gt; &lt;input /&gt; &lt;span /&gt; &lt;/ReactFragment&gt; 返回多个元素,而无需在 div 或 span 中呈现它们

    【讨论】:

    • 我会试试这个,但我担心它仍然会破坏按钮的功能。我不知道为什么,但是在标签内输入 not 给我带来了问题......
    • 哦,对不起!理解相反。
    猜你喜欢
    • 2012-07-02
    • 2014-04-20
    • 1970-01-01
    • 2010-10-03
    • 2016-11-10
    • 1970-01-01
    • 2013-04-12
    • 2023-03-02
    相关资源
    最近更新 更多