728x90
SMALL
#jquery
# jQuery/Method/.addClass() - Method to add class value
.addClass()
You can add class values to selected elements with .addClass() .
Grammar
.addClass( className )
Enclose class values in double or single quotes.
$( 'h1' ).addClass( 'abc' );
You can add multiple values separated by overrides.
$( 'h1' ).addClass( 'ab cd ef' );
As the class value is added while the page is loaded, the visitor can also see the shape change from before it is added to after it is added.
Example
The h1 element adds the bg class to create the background color and the h2 element adds the bg and bd classes to create the background color and border.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery</title>
<style>
.bg { background-color: #eeeeee; }
.bd { border: 1px solid #666666; }
</style>
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$( document ).ready( function() {
$( 'h1' ).addClass( 'bg' );
$( 'h2' ).addClass( 'bg bd' );
});
</script>
</head>
<body>
<h1>Lorem ipsum dolor.</h1>
<h2>Lorem ipsum dolor.</h2>
</body>
</html>
#jquery method #jquery addclass #addClass() #jquery method #jquery grammar #jquery basic
728x90
LIST