<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box {
width: 200px;
height: 200px;
margin: 100px auto 0;
font-size: 24px;
border: 4px solid transparent;
background-color: #eee;
transition: all .3s;
display: flex;
justify-content: center;
align-items: center;
}
.box-border {
font-size: 32px;
border-color: #f00;
}
</style>
</head>
<body>
<div class="box" id="box" οnclick="changeBorder()">我是盒子</div>
<script>
function changeBorder() {
var _box = document.getElementById('box');
var _box_class = _box.getAttribute('class');
if (_box_class == 'box') {
_box.setAttribute('class', 'box box-border');
} else {
_box.setAttribute('class', 'box');
}
}
</script>
</body>
</html>