상세 컨텐츠

본문 제목

jquery toggleClass()

자바스크립트/jquery

by somiyuralove 2021. 4. 9. 14:28

본문

Toggle the class 'highlight' when a paragraph is clicked.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>toggleClass demo</title>
<style>
p {
margin: 4px;
font-size: 16px;
font-weight: bolder;
cursor: pointer;
}
.blue {
color: blue;
}
.highlight {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>

<p class="blue">Click to toggle</p>
<p class="blue highlight">highlight</p>
<p class="blue">on these</p>
<p class="blue">paragraphs</p>

<script>
$( "p" ).click(function() {
$( this ).toggleClass( "highlight" );
});
</script>

</body>

관련글 더보기

댓글 영역