Home >Web Front-end >Front-end Q&A >jquery method to get image height

jquery method to get image height

王林
王林Original
2023-05-28 17:50:411024browse

jQuery is a JavaScript library built on JavaScript, making code written in JavaScript more concise and easy to use. In the process of web development, it is often necessary to obtain information such as the height and width of images. Next we will introduce how to get the height of an image through jQuery.

  1. Get the image height through the img element

In HTML, you can use the img element to embed images. We can use jQuery selector to get the element and get its height attribute. The code is as follows:

var height = $('img').height();

'img' here is a selector used to select picture elements in the document. The height() method is used to get the height of the element and returns a numeric value. Since this method obtains the height of the element, excluding the height of the border and padding, this height value is smaller than the height of the actual image. If you need to get the height including borders and padding, you can use the outerHeight() method.

  1. Get the height of the image through the load event

Another way to get the height of the image is to get its height after the image is loaded. We can use the load() method to bind an event when the image is loaded, and obtain the height of the image in the event handling function. The code is as follows:

$('img').on('load', function() {
  var height = $(this).height();
});

The 'on' method here is used to bind the load event. Note that the bind() method cannot be used here, because the bind() method can only be successfully bound when the image has been loaded, and the load event is triggered after the image is loaded. In the event handling function, we can use $(this) to get the image element that currently triggers the event, and then use the height() method to get the image height.

  1. Get the image height through the newly created Image object

We can also get the height and width of the image by creating a new Image object. The code is as follows:

var img = new Image();
img.onload = function() {
  var height = this.height;
}
img.src = 'image.png';

Here, we first create a new Image object img and set its onload event handler to obtain its height after the image is loaded. Then, we set the src attribute of img to start loading the image.

It should be noted that this method obtains the height of the image through the JavaScript native Image object, rather than obtaining the image element through the jQuery selector. Therefore, if you want to use other methods of jQuery to operate this Image object, you need to convert it to a jQuery object, for example:

$(img).appendTo('body');

The above code converts the created Image object img into a jQuery object, and then adds it to in the document.

To sum up, through the above method, we can get the height of the image through jQuery. Of course, these methods can also be used to obtain the width and other properties of the image. In actual use, we need to choose the appropriate method according to specific needs.

The above is the detailed content of jquery method to get image height. 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
Previous article:jquery tree remove urlNext article:jquery tree remove url