使用纯js更改父母的样式

尤里·雷宾(Yuriy Rybin)

我有一个开关按钮。代码如下:

<div class="switch-radiobutton" data-style="rounded" data-label="on-off">
   <input type="radio" id="rb-1" name="rb" checked>
   <label class="label" for="rb-1"></label>
</div>

<div class="switch-radiobutton" data-style="rounded" data-label="on-off">
    <input type="radio" id="rb-2" name="rb">
    <label class="label" for="rb-2"></label>
</div>

CSS:

/* BASIC SWITCH STYLE  */

.switch-radiobutton [type='radio'] {
    visibility: hidden;
}

.switch-radiobutton {
    display: inline-block;
    position: relative;
    height: 50px;
    line-height: 50px;
    width: 100px;
    margin-bottom: 30px;

    background: red;
    z-index: 1;
}

/* LABEL STYLE  */

.switch-radiobutton:before,
.switch-radiobutton:after {
    display: block;
    position: absolute;
    top: 0;
    height: 100%;
    width: 50%;
    text-align: center;
    text-transform: uppercase;
    font-size: 20px;
    z-index: 2;
    color: #fff;
}

.switch-radiobutton:before {
    left: 0;
}

.switch-radiobutton:after {
    right: 0;
}

.switch-radiobutton {

    &:before {
        content: 'on';
    }

    &:after {
        content: 'off';
    } 
}

/* SWITCH OFF */

.switch-checkbox label,
.switch-radiobutton label {
    position: absolute;
    display: block;
    top: 4px;
    left: 4px;
    height: 42px;
    width: 42px;

    background: #fff;
    cursor: pointer;
    transition: all .4s ease-in-out;
    z-index: 3;
}

/* SWITCH ON */

.switch-checkbox [type='checkbox']:checked + label,
.switch-radiobutton [type='radio']:checked + label {
    transform: translate3d(50px,0,0);
}

/* SWITCH ROUNDED */

.switch-radiobutton[data-style='rounded'],
.switch-radiobutton[data-style='rounded'] label {
    border-radius: 50px;
}

/* SWITCH SQUARE */

.switch-checkbox[data-style='square'],
.switch-checkbox[data-style='square'] label {
     border-radius: 5px;
}

如何更改使用CSS或纯js检查的输入上的父元素(.switch-radiobutton)的背景?没有jQuery。据我所知,我无法使用CSS更改父元素。

文森特·奥利维特·里埃拉(Vincent Olivert Riera)

使用onchange按钮的调用一个函数并将其this作为参数传递

在函数中,用于.parentNode获取作为参数传递的节点的父级。

然后,您可以使用.style.backgroundColor()更改背景。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章