我想在选中单选按钮时将元素的位置更改为左边。但此代码无效。
if(document.getElementById("red").checked){
document.querySelector(".sliding-content").style.left = "-100%";
}
答案 0 :(得分:0)
使用JQuery,您可以根据单击单选按钮移动元素。
$("#redRadio").click(function() {
$('#test').css('margin-left', '100px');
});

#test {
height: 100px;
width: 100px;
background-color: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<input id="redRadio" type="radio" name="color" value="red">Red<br>
</form>
<div id="test"></div>
&#13;
根据要求,纯JS:
function moveRed() {
document.getElementById("test").style.marginLeft = "100px";
}
&#13;
#test {
height: 100px;
width: 100px;
background-color: red;
}
&#13;
<form action="">
<input onclick="moveRed()" id="redRadio" type="radio" name="color" value="red">Red<br>
</form>
<div id="test"></div>
&#13;