<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 200px;
height: 200px;
color: red;
margin: 10px;
}
.box1{
border: 1px solid #ddd;
}
.box2{
border: 1px solid blue;
}
</style>
</head>
<body>
<div title="1">
你好
</div>
<div title="2">
hello
</div>
<script src="lib/jquery-2.2.2.js"></script>
<script>
//class类的操作:
// 1.addClass(class名)可以有多个class名,用空格分开
$('div').addClass('box box1');
// 2.removeClass(class名)可以有多个class名,用空格隔开,如果无参,则元素上的class全部删除
$('div').removeClass('box1 box');
// 3.toggleClass(class名)切换,如果有class则remove,没有则add
$('div').toggleClass('box1');
$('div').addClass(function(){
return 'box box1'+ $(this).attr('title');
});
</script>
</body>
</html>