直接上代码
<!DOCTYPE html>
<html>
<head>
<style>
.info {
color: green;
}
</style>
</head>
<body>
<input type="button" value="点击" onclick="changeClass()" />
<div id="x1">例子</div>
<script src="js/jquery-3.7.1.min.js"></script>
<script>
function changeClass() {
// toggleClass:有就移除,没有就添加
$("#x1").toggleClass('info')
}
</script>
</body>
</html>
在浏览器上,点击按钮就能看到文字的颜色变化
点个赞再走呗~