How to change div height using jquery

青灯夜游
Release: 2022-06-08 16:27:13
Original
4272 people have browsed it

3 methods: 1. Use "$("div").height(height)" to set a new height value; 2. Use "$("div").css("height" ,"height value")", add a new height style; 3. Use "$("div").attr("style","height:value")".

How to change div height using jquery

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

There are many ways to modify the height of elements in jquery:

  • height()

  • css()

  • attr()

1. Use height() to modify the div height

height() method can set the height of matching elements. Syntax:

$("div").height(高度值)
Copy after login

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-3.2.1.min.js"></script>
		<script>
			$(document).ready(function() {
				$("#but1").click(function() {
					$("div").height("30px");
				});
				$("#but2").click(function() {
					$("div").height("100px");
				});
			});
		</script>
		<style>
			div{
				height: 60px;
				background-color: #FFC0CB;
			}
		</style>
	</head>
	<body>

		<div>
			一个div元素,高度60px
		</div>
		<br>
		<button id="but1">减小div高度</button>
		<button id="but2">增大div高度</button>
	</body>
</html>
Copy after login

How to change div height using jquery

2. Use css() to modify the div height

Use css () Add height style to td element, syntax:

$("div").css("height","高度值");
Copy after login

Example: Modify

$(document).ready(function() {
	$("button").click(function() {
		$("div").css("height","40px");
	});
});
Copy after login

How to change div height using jquery

3 based on the example of method 1. 3. Use attr() modifies the div height

Use attr() to control the style attribute value and add the height style to the td element. Grammar:

$("div").attr("style","height:高度值")
Copy after login

Example: Modify

$(document).ready(function() {
	$("button").click(function() {
		$("div").attr("style","height:100px")
	});
});
Copy after login

How to change div height using jquery

based on the example of method 1 [Recommended learning: jQuery video tutorial,webfront-end video

The above is the detailed content of How to change div height using 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