Home >Web Front-end >Front-end Q&A >How to hide adjacent elements of clicked elements in jquery

How to hide adjacent elements of clicked elements in jquery

青灯夜游
青灯夜游Original
2022-03-25 18:05:011666browse

Method: 1. Use click() to bind the click event to the element and set the processing function; 2. In the processing function, use the next() and prev() functions to obtain adjacent elements, and use hide() ) will hide the obtained element, the syntax is "element.next().hide(); element.prev().hide()".

How to hide adjacent elements of clicked elements in jquery

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

jquery implements clicking on an element and hiding its adjacent elements

Implementation method:

  • Use The click() function binds a click event to the specified element and sets the processing function

  • In the processing function, use the next() and prev() functions to obtain adjacent elements, and use hide () function hides the obtained element

    • next(): used to obtain the next sibling element.

    • prev(): used to get the previous sibling element.

Implementation code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
		</style>
		<script>
			$(document).ready(function() {
				$("li.start").css({
					"color": "red",
					"border": "2px solid red"
				});
				$("li.start").click(function() {
					$("li.start").next().hide();
					$("li.start").prev().hide();
				});
			});
		</script>
	</head>
	<body>

		<div style="width:500px;" class="siblings">
			<ul>ul (父节点)
				<li>li </li>
				<li>li (类名为"star"的相邻元素)</li>
				<li class="start">类名称为“star”的li元素</li>
				<li>li (类名为"star"的相邻元素)</li>
				<li>li </li>
			</ul>
		</div>
		<p>点击类名称为“star”的li元素,隐藏其相邻的两个元素</p>
	</body>
</html>

How to hide adjacent elements of clicked elements in jquery

##[Recommended learning:

jQuery video tutorialwebfront-end video

The above is the detailed content of How to hide adjacent elements of clicked elements 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