addclass
<!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>溫故而知"心"</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="demo"></div>
<div class="demo"></div>
<div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.demo').eq(0).addClass('active');
$('.demo').eq(1).addClass('active xyz');
$('.demo').addClass('state');
$('.demo').addClass(function(index, ele){
if( (index+1) % 2 == 0){
return 'demo2'
}
})
</script>
</html>
removeClass
<!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>溫故而知"心"</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="demo demo1 active"></div>
<div class="demo demo1 active"></div>
<div class="demo demo1"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.demo').eq(0).removeClass('active');
$('.demo').eq(0).removeClass('active demo');
$('div').eq(1).removeClass();
$('div').removeClass(function (index, ele) {
if ((index + 1) % 2 == 0) {
return 'demo1'
}
})
$('div').removeClass()
</script>
</html>
hasClass
<!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>溫故而知“心”</title>
<style>
.demo {
width: 100px;
height: 100px;
background-color: #0000ff;
margin-bottom: 10px;
}
.demo.active {
background-color: #ff0000;
}
</style>
</head>
<body>
<div class="demo active"></div>
<div class="demo"></div>
<div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
console.log($('.demo').hasClass('active'));
$('.demo').click(function () {
$('.demo').removeClass('active');
$(this).addClass('active')
})
</script>
</html>
<!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>溫故而知“心”</title>
<style>
.wrapper .style1 {
background-color: #ff8c00;
}
.wrapper .style1 li {
color: #ff0000;
}
.wrapper .style2 {
background-color: #0000ff;
}
.wrapper .style2 li {
color: #006400;
}
.wrapper.active .style1 {
background-color: #ff0000;
}
.wrapper.active .style1 li {
color: #ff8c00;
}
.wrapper.active .style2 {
background-color: #006400;
}
.wrapper.active .style2 li {
color: #0000ff;
}
</style>
</head>
<body>
<div class="wrapper">
<ul class="style1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul class="style2">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('.wrapper').click(function (ele, index) {
if ($(this).hasClass('active')) {
$(this).removeClass('active')
} else {
$(this).addClass('active')
}
})
</script>
</html>