.addClass()

  • .addClass(): thêm class cho thành phần.

.addClass(function(index, class_đã_có){…})

.addClass(tênclass)

.addClass(tênclass01 tênclass02)

.addClass(function)

<!DOCTYPE html>
<html>
<head>
	<title>.addClass() Demo</title>
	<script type="text/javascript" src="jquery-3.4.1.min.js"></script>
	<style type="text/css">
		p{
			margin: 8px;
			font-size: 16px;
		}
		.selected {
			color: red;
		}
		.highlight{
			background: yellow;
		}

		div{
			background: white;
			border: 1px solid green;
		}
		li{
			background: yellow;
		}
		li.bdrred{
			background: red;
		}

	</style>
</head>
<body>
	<!-- Ví dụ 1 add theo tên class -->
	<div>Ví dụ 1 add theo tên class
		<p>Hello</p>
		<p>and</p>
		<p>Goobye</p>
	</div>

	<!-- Ví dụ 2 add theo tên index -->
	<div>Ví dụ 2 add theo tên index 
		<ul>
			<li>Item1</li>
			<li>Item2</li>
			<li class="blue">Item3</li>
			<li>Item4</li>
			<li>Item5</li>
		</ul>
	</div>


	<script type="text/javascript">
		$("p").last().addClass("highlight").addClass("selected");

		$(function(){
			$('ul li').addClass(function(index,current){
				var newClass;
				if (current == "blue") {
					newClass = "bdrred";
				}
				return newClass;
			});
		});
	</script>
</body>
</html>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started