How to hide child elements in jquery

青灯夜游
Release: 2022-03-01 17:51:56
Original
2639 people have browsed it

Jquery method of hiding child elements: 1. Use the children() and hide() methods, the syntax is "$("parent element").children().hide()"; 2. Use find() And hide() method, syntax "$("parent element").find("child element").hide()".

How to hide child elements in jquery

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

jquery hidden child elements

Implementation method:

  • First you need to select the child elements

    • children() method: Get the direct subset elements under this element

    • find() method: Get all (including children) under this element A subset of the set) subset element

  • and then use the hide() method to hide the selected child elements.

1. Use the children() and hide() methods

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("button").click(function() {
					$("ul").children().hide();
				});
			});
		</script>
		<style>
			.intro {
				border: 1px solid red;
			}
		</style>
	</head>
	<body>
		<ul class="intro ">
		  <li>list1
		    <ul>
			      <li>list1-1</li>
			      <li>list1-2</li>
			  </ul>
			</li>
		  <li>list2
		    <ul>
			      <li>list2-1</li>
			      <li>list2-2</li>
			  </ul>
			</li>
		  <li>list3
		    <ul>
			      <li>list3-1</li>
			      <li>list3-2</li>
			  </ul>
			</li>
		</ul>
		<button>隐藏子元素</button>
	</body>
</html>
Copy after login

How to hide child elements in jquery

2. Use the find() and hide() methods

$(document).ready(function() {
	$("button").click(function() {
		$("ul").find("li").hide();
	});
});
Copy after login

[Recommended learning: jQuery video tutorial, web front-end development video]

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