【问题标题】:Changing the Style of a specific element by Checkbox:Checked通过 Checkbox:Checked 更改特定元素的样式
【发布时间】:2021-10-14 19:57:33
【问题描述】:

我使用复选框来修改其他元素的样式。复选框是否有可能影响网站的其他类/元素。

我在下面有一个示例 html 代码,而不是更改标签元素,是否可以更改第一个 div 的样式。

ul li {
  display: inline-block;
}

ul li input[type="checkbox"]:checked+label {
  border: 2px solid gray;
  background-color: gray;
  color: #fff;
  transition: all .2s;
}

ul li {
  padding: 20px;
  margin: 20px;
}

ul li input[type="checkbox"] {
  display: absolute;
}

ul li input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

ul li label {
  padding: 20px;
  cursor: pointer;
}
<div class="modify-box">
  BOX TO CHANGED
</div>
<ul>
  <li>
    <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
    <label for="vehicle1"> Bike</label><br>
  </li>
  <li>
    <input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
    <label for="vehicle2"> Car</label><br>
  </li>
  <li>
    <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
    <label for="vehicle3"> Boat</label><br>
  </li>
</ul>

【问题讨论】:

  • 如果你想要第一个父 div 那么 css 没有父选择器
  • 你还需要使用Javascript
  • 不,这是不可能的。当前的 CSS 只允许在 DOM 结构中向下和向右选择,而不是向上或向左选择。另见:Is there a CSS parent selector?
  • 但是如果您可以修改 HTML 结构,那么您可以将实际的复选框输入放在 元素之前 - 无论如何它们在视觉上是隐藏的,标签可以保留在它们所在的位置. 然后您可以根据复选框的状态选择 div 元素。

标签: html css checkbox styling


【解决方案1】:

有一种方法可以实现这一点,但它使用与设置&lt;label&gt; 元素样式完全相同的“技巧”,特别是将&lt;input&gt; 元素移动到要设置样式的元素之前。

考虑到这一点,如果 &lt;input&gt; 元素是 &lt;div&gt; 的前同级元素,则选中和取消选中 &lt;input&gt; 可以对 &lt;div&gt; 以及原始 &lt;label&gt; 产生影响元素也是如此。

作为一个粗略的例子:

input[type="checkbox"][name^="vehicle"] {
  display: absolute;
  position: absolute;
  opacity: 0;
}

/* styles the <div> based on the checked/unchecked state
   of the <input> (this example assumes that the same
   highlight colour should be used regardless of which
   <input> is checked: */
input[type="checkbox"][name^="vehicle"]:checked ~ div {
  background-color: #ccc;
}

/* this is where it becomes obvious that JavaScript (or,
   ideally, a CSS selector that can refer to an attribute-
   variable) makes more sense; though with a CSS
   pre-processor this can be written effectively enough.
   Here when the #vehicle1 element is checked the <label>
   descendents with a "for" attribute equal to "vehicle1" of
   later-sibling <ul> elements are selected and styled: */
#vehicle1:checked~ul label[for=vehicle1] {
  background-color: gray;
}

/* as above, for the "vehicle2" id and for attributes: */
#vehicle2:checked~ul label[for=vehicle2] {
  background-color: gray;
}

#vehicle3:checked~ul label[for=vehicle3] {
  background-color: gray;
}

ul li {
  display: inline-block;
  padding: 20px;
  margin: 20px;
}

ul li label {
  padding: 20px;
  cursor: pointer;
}
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<div class="modify-box">
  BOX TO CHANGED
</div>
<ul>
  <li>
    <label for="vehicle1"> Bike</label><br>
  </li>
  <li>
    <label for="vehicle2"> Car</label><br>
  </li>
  <li>
    <label for="vehicle3"> Boat</label><br>
  </li>
</ul>

【讨论】:

    【解决方案2】:

    仅使用 CSS 是不可能的。您受到当前选择器的限制,并且 CSS 中没有父选择器。你可以使用一点 JS 来定位你想要的元素。

    您希望在复选框周围的框中发生什么?也许会有另一种解决方案,标签上带有:before:after

    【讨论】:

      【解决方案3】:

      我认为你需要 JavaScript。如果没有选中复选框,您可以监听 change 事件和样式。


      首先,您需要选择包含列表元素并为其附加一个事件侦听器:

      document.querySelector('ul').addEventListener('change', function(e) {...});
      

      在侦听器的处理函数中,您必须检查是否有选中的复选框,并根据该检查样式您的.modify.box(或切换一个类,例如.checked):

      if (document.querySelector('input:checked')) {
        modify_box.classList.add('checked');
      }
      else {
        modify_box.classList.remove('checked');
      }
      

      如果您决定切换一个类,您需要将该类添加到您的 CSS 定义中。例如:

      ul li input[type="checkbox"]:checked+label,
      .checked {...}
      

      工作示例:

      const modify_box = document.querySelector('.modify-box');
      
      document.querySelector('ul').addEventListener('change', function(e) {
        if (document.querySelector('input:checked')) {
          modify_box.classList.add('checked');
        }
        else {
          modify_box.classList.remove('checked');
        }
      });
      ul li {
        display: inline-block;
      }
      
      ul li input[type="checkbox"]:checked+label,
      .checked {
        border: 2px solid gray;
        background-color: gray;
        color: #fff;
        transition: all .2s;
      }
      
      ul li {
        padding: 20px;
        margin: 20px;
      }
      
      ul li input[type="checkbox"] {
        display: absolute;
      }
      
      ul li input[type="checkbox"] {
        position: absolute;
        opacity: 0;
      }
      
      ul li label {
        padding: 20px;
        cursor: pointer;
      }
      <div class="modify-box">
        BOX TO CHANGED
      </div>
      <ul>
        <li>
          <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
          <label for="vehicle1"> Bike</label><br>
        </li>
        <li>
          <input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
          <label for="vehicle2"> Car</label><br>
        </li>
        <li>
          <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
          <label for="vehicle3"> Boat</label><br>
        </li>
      </ul>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 1970-01-01
        • 2016-06-03
        相关资源
        最近更新 更多