如何通过单选按钮更改输入父div的位置?

时间:2018-08-12 10:33:57

标签: html css

在一个容器中,放置了两个单选按钮输入。当选中static按钮时,包含的div应该为positioned静态,当选中absolute按钮时,容器的位置应为absolute。如何实现此无JavaScript代码?我需要额外的标记吗?

div.container { border: 1px solid black; }

div.container input[id="static"]:checked + label {
    background-color: aqua;
}

div.container input[id="absolute"]:checked + label {
    background-color: green;
}
        <div class="container">
            <input id="static" type="radio" name="position" checked />
            <label for="static">Static</label>

            <input id="absolute" type="radio" name="position" />
            <label for="absolute">Absolute</label>
        </div>

1 个答案:

答案 0 :(得分:0)

使用onchange更改container样式

function changePosition(pos){
var cont = document.getElementsByClassName("container")[0];
cont.style.position=pos;
}
div.container { border: 1px solid black; }

div.container input[id="static"]:checked + label {
    background-color: aqua;
}

div.container input[id="absolute"]:checked + label {
    background-color: green;
}
<div class="container">
  <input id="static" type="radio" name="position" checked onchange="changePosition('static')" />
   <label for="static">Static</label>

   <input id="absolute" type="radio" name="position" onchange="changePosition('absolute')"/>
   <label for="absolute">Absolute</label>
</div>