<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.redioBox {
width: 120px;
height: 30px;
margin: auto;
display: flex;
justify-content: space-between;
border-radius: 20px;
background-color: #f5f5f5;
}
.redioBtn {
width: 50%;
height: 100%;
border-radius: 20px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
overflow: hidden;
}
.redioBtnChen {
background-color: rgb(15, 57, 245);
color: #fff;
}
.redioBtn p {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="redioBox">
<div class="redioBtn">
<p id="man">男</p>
</div>
<div class="redioBtn">
<p id="woman">女</p>
</div>
</div>
</body>
</html>
<script>
//添加类名:document.getElementById("myDiv").classList.add('mystyle');
//删除类名:document.getElementById("myDiv").classList.remove('mystyle');
let $ = (str) => {
return document.querySelector(str);
}
let redionFun = () => {
$('#woman').classList.add('redioBtnChen');
$('#man').onclick = () => {
$('#man').classList.add('redioBtnChen');
$('#woman').classList.remove('redioBtnChen');
}
$('#woman').onclick = () => {
$('#woman').classList.add('redioBtnChen');
$('#man').classList.remove('redioBtnChen');
}
}
redionFun();
</script>