【问题标题】:How do I use CSS attribute selector with the adjacent selector?如何将 CSS 属性选择器与相邻选择器一起使用?
【发布时间】:2015-03-17 05:26:06
【问题描述】:

对于下面的 CSS,我如何共同使用属性和相邻选择器?

.onoff-checkbox:checked + .onoff-label .onoff-inner {margin-left: 0;}
.onoff-checkbox:checked + .onoff-label .onoff-switch {right: 0px;}

我正在使用它来创建 CSS 切换样式复选框,并且希望在页面上拥有多个复选框?使用以下 HTML 代码(示例):

<input type="checkbox" name="onoff" class="onoff-checkbox-1" id="myonoff" checked>
...
<input type="checkbox" name="onoff" class="onoff-checkbox-2" id="myonoff" checked>

我已经尝试了以下 -

input[class*="onoff-checkbox"]:checked + label[class*="onoff-label"] span[class*="onoff-inner"]
...

什么是正确的语法。感谢您的帮助。

这是试用的代码 - https://jsfiddle.net/vedanta_/8z3sqatf/ 原代码改编自-https://proto.io/freebies/onoff/

【问题讨论】:

  • 需要查看更多 HTML。您显示的内容不足以代表您拥有的选择器 - 即缺少标签。

标签: html css css-selectors


【解决方案1】:

您不能对多个元素使用相同的id。您可以使用 myonoff1myonoff2

然后你可以像这样使用 CSS 访问它

#myonoff1
{
    ...
}

#myonoff2
{
    ...
}

在 jQuery 中,你可以使用这个:

$('#myonoff1').show(); // or any other method.

另外,除了使用div[class*="onoff"] {,您还可以坚持使用元素共享的类的模式。例如,在 HTML 中使用 class="onoff-checkbox",在 CSS 中使用 .onoff-checkbox { ... }

【讨论】:

  • 我可以使用类来引用一个元素,当我使用代码和唯一编号生成元素时,它可以用作通配符 - 参见示例 - jsfiddle.net/vedanta_/15702n8z - 我现在想添加相邻的选择器' +'
  • 唯一性是 id 的用途。使用类是可能的,但我强烈建议您坚持使用 id 和类的正确模式。
【解决方案2】:

在这个例子中,我认为你对什么必须是独一无二的感到困惑。

(如果我错了,有人纠正我),但我认为元素的 id 应该与元素的 name 相同,并且它们是唯一的标识符输入元素。标签的 for 属性应该准确地反映这些。无论如何,元素不能共享 id,也不能共享 for 属性。

这些类绝对可以在输入元素之间共享——它们不必是唯一的。所以为了解决这个问题,我稍微修正了你的标记。

div[class*="onoff"] {
    position: relative;
    width: 90px;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select: none;
}
input[class*="onoff-checkbox"] {
    display: none;
}
label[class*="onoff-label"] {
    display: block;
    overflow: hidden;
    cursor: pointer;
    border: 2px solid #999999;
    border-radius: 20px;
}
span[class*="onoff-inner"] {
    display: block;
    width: 200%;
    margin-left: -100%;
    -moz-transition: margin 0.3s ease-in 0s;
    -webkit-transition: margin 0.3s ease-in 0s;
    -o-transition: margin 0.3s ease-in 0s;
    transition: margin 0.3s ease-in 0s;
}
span[class*="onoff-inner"]:before, span[class*="onoff-inner"]:after {
    display: block;
    float: left;
    width: 50%;
    height: 30px;
    padding: 0;
    line-height: 30px;
    font-size: 14px;
    color: white;
    font-family: Trebuchet, Arial, sans-serif;
    font-weight: bold;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
span[class*="onoff-inner"]:before {
    content:"Yes";
    padding-left: 10px;
    background-color: #34A7C1;
    color: #FFFFFF;
}
span[class*="onoff-inner"]:after {
    content:"No";
    padding-right: 10px;
    background-color: #EEEEEE;
    color: #999999;
    text-align: right;
}
span[class*="onoff-switch"] {
    display: block;
    width: 18px;
    margin: 6px;
    background: #FFFFFF;
    border: 2px solid #999999;
    border-radius: 20px;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 56px;
    -moz-transition: all 0.3s ease-in 0s;
    -webkit-transition: all 0.3s ease-in 0s;
    -o-transition: all 0.3s ease-in 0s;
    transition: all 0.3s ease-in 0s;
}
input[class*="onoff-checkbox"]:checked {
    margin-left: 0;
}
label[class*="onoff-label"] {
    margin-left: 0;
}
span[class*="onoff-inner"] {
    margin-left: 0;
}
input[class*="onoff-checkbox"]:checked {
    right: 0px;
}
label[class*="onoff-label"] {
    right: 0;
}
span[class*="onoff-inner"] {
    right: 0;
}
input.onoff-checkbox:checked + label.onoff-label span.onoff-switch {
    right: 5px;
}
<div class="onoff">
    <input type="checkbox" name="onoff" class="onoff-checkbox" id="onoff" checked="true" />
    <label class="onoff-label" for="onoff"> 
        <span class="onoff-inner"></span>
        <span class="onoff-switch"></span>            
    </label>
</div>
<br/>
<div class="onoff2">
    <input type="checkbox" name="onoff2" class="onoff-checkbox" id="onoff2" checked="true" />
    <label class="onoff-label" for="onoff2">
        <span class="onoff-inner2"></span>
        <span class="onoff-switch"></span>                
    </label>
</div>

CSS 中要关注的主要内容是这一行:

input.onoff-checkbox:checked + label.onoff-label span.onoff-switch {
    right: 5px;
}

这以.onoff-checkbox 类的元素为目标,然后根据该值应用样式。

您可以在 working JSFiddle 中看到这一点。

【讨论】:

  • 元素不必具有相同的 id 和 name 属性。 for 属性仅引用 id。
  • @BoltClock 我认为 for 属性是用于名称的?但我的观点是 id 可以与名称相同,或者通常应该是。
  • 不,for属性肯定是指id。
  • @BoltClock 那么如果已经有一个唯一标识符,那么这个名字有什么作用呢?
  • 该名称用于表单提交。我不知道他们引用 id 的实际原因 - 如果我猜的话,它似乎与 IDREF 的 XML 概念有关(除非 XML 从 HTML 借用 that,在这种情况下我很困惑)。
猜你喜欢
  • 2021-09-05
  • 2016-01-13
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2011-10-16
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多