Two ways to get the height of a picture in a Web page using jquery _jquery

Source: Internet
Author: User
Using jquery to get the height of a picture in a Web page is really simple, and there are two common ways to hit our goal.
Copy Code code as follows:

$ ("img"). whith (); (return pure digits)
$ ("img"). CSS ("width"); (return string: number + "px")

But sometimes you will get a return of 0, the above method return value unexpectedly is 0 or 0px, very surprising

Method One

Before very early, I used the solution, this is the solution that my master told me: Add the width attribute to the label of the picture you need to get, or write the picture in the CSS to tell it, so it's OK, so every time I want to get a picture's height, All need to first measure the height of the picture, and then write to the Web page, so that can, is not very clumsy ah, let's look at the second method.

Method Two

Recently looking at learning jquery English version of the original, just because one side of the translation, while reading, so every page is very careful, so finally read the following two kinds of commonly used jquery event loading methods
Copy Code code as follows:

$ (function () {});
Window.onload=function () {}

The first one is called after the DOM structure is rendered, when some resources in the Web page have not been loaded, such as pictures and other resources, but the DOM structure has been successfully rendered.
The second one is called after the Web page DOM structure is rendered and the resource has been successfully loaded.

Do you feel the difference? One is invoked when the resource is not loaded, one is called after the resource is loaded, and the page has been rendered, so when we call $ (' the function () {} ') the $ (' img '). Width (), because the picture has not yet loaded, so this time the height of the label is 0, so the return value is 0. But when you call with Window.onload=function () {}, the picture is loaded, so you can get the height of the picture.

So remember, the $ (function () {}) is executed at the end of the DOM rendering, when the resource is not loaded, and if you want to get some information about the resources, there is no way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.