Home > Web Front-end > Front-end Q&A > How to use width method in jquery

How to use width method in jquery

WBOY
Release: 2022-06-01 18:18:07
Original
2267 people have browsed it

In jquery, the width() method is used to set or return the width of the specified element. The syntax is "element object.width()" or "element object.width (set width)"; when the width is returned When , the returned result is the width of the first matched element.

How to use width method in jquery

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

How to use the width method in jquery

width() method sets or returns the width of the selected element.

When this method is used to return the width, it returns the width of the first matching element.

When this method is used to set the width, the width of all matching elements is set.

As shown in the image below, this method does not contain padding, border or margin.

How to use width method in jquery

Return width:

$(selector).width()
Copy after login

Set width:

$(selector).width(value)
Copy after login

Use function to set width:

$(selector).width(function(index,currentwidth))
Copy after login

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("div的宽度: " + $("div").width());
});
});
</script>
</head>
<body>
<div style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div><br>
<button>显示div的宽度</button>
</body>
</html>
Copy after login

Output result:

How to use width method in jquery

After clicking the button:

How to use width method in jquery

Related video tutorial Recommended: jQuery video tutorial

The above is the detailed content of How to use width method 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