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

青灯夜游
Release: 2022-03-25 18:05:01
Original
1669 people have browsed it

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template