在一个容器中,放置了两个单选按钮输入。当选中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>
答案 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>