【问题标题】:Custom radio buttons to show/hide content自定义单选按钮以显示/隐藏内容
【发布时间】:2021-03-19 20:15:50
【问题描述】:

我正在尝试构建一个交互式电子邮件内容块,该块将根据选择显示或隐藏内容。在我尝试自定义标签之前它起作用了。在我将输入和标签包装在 div 中后,它停止工作。我认为这可能会使我的选择器关闭,但我不确定如何解决这个问题。

.custom-radios input[type="radio"] + label span img {
  opacity: 0;
  transition: all 0.3s ease;
}
.custom-radios input[type="radio"]#red + label span {
  background-color: red;
}
.custom-radios input[type="radio"]#blue + label span {
  background-color: blue;
}
.custom-radios input[type="radio"]#green + label span {
  background-color: green;
}
.custom-radios input[type="radio"]#orange + label span {
  background-color: orange;
}
.custom-radios input[type="radio"]:checked + label span img {
  opacity: 1;
}


    #red{
    display: none
    }
    #blue{
        display: none
    }
    #green{
        display: none
    }
    #orange{
        display: none
    }
    input[type="red"]:checked ~ #red {
    display: block
    }

    input[value="blue"]:checked ~ #blue {
        display: block;
    }

    input[value="green"]:checked ~ #green {
        display: block;
    }

    input[value="orange"]:checked ~ #orange {
        display: block;
    }
<div class="custom-radios">

  <div>
    <input type="radio" id="red" name="color" value="red">
    <label for="red">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="blue" name="color" value="blue">
    <label for="blue">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="green" name="color" value="green">
    <label for="green">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div>
    <input type="radio" id="orange" name="color" value="orange">
    <label for="orange">
      <span>
        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
      </span>
    </label>
  </div>

  <div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

<p style="margin:0;" id="red">
<img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="blue">
<img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="green">
<img src="https://via.placeholder.com/580x200/00FF00/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="orange">
<img src="https://via.placeholder.com/580x200/FFA500/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>

<div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

</div>

【问题讨论】:

  • 因为选择器a ~ b 表示b 位于同一父级中的a 之后,所以它已损坏。您的#red#blue 等属于与input[...]:checked 不同的父级。查看 w3schools 的 CSS Selectors Reference,它可能会对您有所帮助。
  • 嗨,Leo,我整天都在做这个,但仍然无法解决这个问题。但是当我将图像部分包装在一个 div 中并将input[type="red"]:checked ~ #red { display: block } 更改为input[type="red"]:checked ~ div #red { display: block } 时,它起作用了。但是当我将收音机部分包裹在一个 div 中时,它又坏了

标签: html css email radio-button


【解决方案1】:

我认为选择器可能是你的弱点,我再次鼓励你检查 w3school 的CSS Selectors Reference

无论如何,我制作了一个简化版本,尽可能与您当前的结构相似,因此您可以看到它的工作原理。

.hidden_input {
  display: none;
}

#red_input+label>span {
  background-color: red;
}

#blue_input+label>span {
  background-color: blue;
}

label>span>img {
  opacity: 0;
  transition: all 0.3s ease;
}

.hidden_input:checked+label>span>img {
  opacity: 1;
}

#red_content,
#blue_content {
  display: none;
}

#red_input:checked~#red_content,
#blue_input:checked~#blue_content {
  display: block;
}
<div class="custom-radios">
  <input type="radio" id="red_input" class="hidden_input" name="color" value="red">
  <label for="red_input">
    <span>
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
    </span>
  </label>
  <input type="radio" id="blue_input" class="hidden_input" name="color" value="blue">
  <label for="blue_input">
    <span>
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
    </span>
  </label>

  <div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;">&nbsp;</div>

  <p style="margin:0;" id="red_content">
    <img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
  </p>
  <p style="margin:0;" id="blue_content">
    <img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
  </p>
</div>

您还可以在 HTML 代码中的任意位置播放和放置元素,然后使用 grid 设置它们的位置(这只是另一种选择)。

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2013-01-20
    相关资源
    最近更新 更多