Example of height of method usage in jQuery

  • 2020-05-09 18:08:14
  • OfStack

This article illustrates the use of the height() method in jQuery as an example. Share with you for your reference. The specific analysis is as follows:

This method gets or sets the height value of the matching element, with the default unit being px.

Grammatical structure 1:

$(selector).height()

Returns the current height of the first matched element with no arguments.
Example code:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="//www.ofstack.com/" />
<title> The home of the script </title>
<style type="text/css">
div{
  height:150px;
  width:150px;
  background-color:green;
  margin-top:10px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  alert($("div").height());
})
</script>
</head>
<body>
<div></div>
</body>
</html>

Grammatical structure 2:

$(selector).height(val)

The default unit is px. You can also use other units such as em or percentages.
Example code:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="//www.ofstack.com/" />
<title> The home of the script </title>
<style type="text/css">
div{
  height:150px;
  width:150px;
  background-color:green;
  margin-top:10px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("div").height("200px")
  })
})
</script>
</head>
<body>
<div></div>
<button> Click to view </button>
</body>
</html>

I hope this article is helpful for you to design jQuery program.


Related articles: