这段代码初始没有加 active,根据代码需要可以初始添加 active 当作默认项
话不多说,上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<style>
.active {
background-color: #99f;
}
</style>
</head>
<body>
<button class="btn">普通按钮样式</button>
<button class="btn">普通按钮样式</button>
<button class="btn">普通按钮样式</button>
</body>
<script type="text/javascript">
$(".btn").click(function(){
$(".btn").removeClass("active");
$(".btn").html("普通按钮样式");
$(this).addClass("active");
$(this).html("选中按钮样式");
})
</script>
</html>