Home  >  Article  >  Web Front-end  >  How to add click event to a certain class element in jquery

How to add click event to a certain class element in jquery

青灯夜游
青灯夜游Original
2021-11-25 18:47:476483browse

Added method: 1. Use the "$(".class name")" statement to obtain the element node that is set to a specified class; 2. Use "element node.click(function(){... })" statement adds a click event to the obtained element node.

How to add click event to a certain class element in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, $() is used to select elements. When using a class, you can use $(".class name") to get and set a certain class. Elements.

Then you can use the click() method to add a click event to the obtained element.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">

		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function(){
				$(".box").click(function(){
					$(".box").css("background-color","#FFC0CB");
				});
			});
		</script>
	</head>
	<body>
		<p class="box">2324 </p>
		<button class="box">点击,修改按钮的样式</button>
	</body>
</html>

How to add click event to a certain class element in jquery

Description: The

click() method triggers the click event, or specifies the function to be run when the click event occurs.

Syntax: $(selector).click(function)

function: Optional. Specifies a function to run when a click event occurs.

Recommended related video tutorials: jQuery Tutorial (Video)

The above is the detailed content of How to add click event to a certain class element in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn