Home  >  Article  >  Web Front-end  >  How to modify the style attribute in jquery to hide elements

How to modify the style attribute in jquery to hide elements

青灯夜游
青灯夜游Original
2022-05-17 19:44:413064browse

Two hiding methods: 1. Use css() to control the display style, the syntax is "element object.css('display','none')"; 2. Use attr() to control the display style, the syntax is " Element object.attr('style','display:none')".

How to modify the style attribute in jquery to hide elements

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

Two methods for jquery to modify the style attribute to hide elements

  • css() method

  • attr() method

1. Use css() method

Use css() to control display or visibility Style, syntax:

元素对象.css('display','none');//隐藏
元素对象.css('visibility','hidden');//元素隐藏

Example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-3.2.1.min.js"></script>
		<style>
			.siblings * {
				display: block;
				border: 2px solid lightgrey;
				color: lightgrey;
				padding: 5px;
				margin: 15px;
			}
			.start{
				color: peru;
			}
		</style>
		<script>
			$(document).ready(function() {

				$("button").click(function() {
					$("p").css(&#39;display&#39;,&#39;none&#39;);//隐藏
					$("div").css(&#39;visibility&#39;,&#39;hidden&#39;);//元素隐藏
				});
			});
		</script>
	</head>
	<body>

		<p>一个p元素</p>
		<div>一个div元素</div>
		<p>一个p元素</p>
		<button>隐藏元素</button>
	</body>
</html>

How to modify the style attribute in jquery to hide elements

#2. Use attr() method

Use attr() to control display or visibility style, syntax:

元素对象.attr(&#39;style&#39;,&#39;display:none&#39;);//隐藏
元素对象.attr(&#39;style&#39;,&#39;visibility:hidden&#39;);//元素隐藏

Example:

$(document).ready(function() {
	$("button").click(function() {
		$("p").attr(&#39;style&#39;,&#39;display:none&#39;);//隐藏
		$("div").attr(&#39;style&#39;,&#39;visibility:hidden&#39;);//元素隐藏
	});
});

How to modify the style attribute in jquery to hide elements

[Recommended learning:

jQuery video tutorialwebfront-end video

The above is the detailed content of How to modify the style attribute in jquery to hide elements. 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