Use jquery to get the Image Height on a webpage-Unlock

Source: Internet
Author: User
JQuery is actually very easy to get the height of images on a webpage using jquery. There are two common methods to get the Image Height on a webpage $ (& quot; img & quot ;) whith (); (returns a pure number) $ (& quot; img & quot;) css (& quot; width & quot;); (returns a string: Number + & quot; px & quot;) But jQuery is used to get the Image Height on a webpage. It is actually very easy to use jquery to get the Image Height on a webpage, there are two common methods that can be used to reach our goal $ ("img "). whith (); (returns a pure number) $ ("img" ).css ("width"); (returns a string: Number + "px") but sometimes returns 0, the returned value of the above method is 0 or 0 PX, which is quite surprising. A very long time ago, I used the solution. This is also the solution that my master told me: add the width attribute to the tag of the image you want to obtain, or write the image description in css. When I want to get the height of an image, I need to first measure the height of a piece and then write it to the webpage. Is it clumsy, next let's look at the second method. Method 2: I recently read the Learning jQuery English version of the original book. Because I translated and read it, I read every page carefully, so I finally carefully read the following two common jquery event Loading Methods: $ (function () {}); window. onload = function () {} is called after DOM structure rendering. At this time, some resources in the webpage have not been loaded, such as slice, but the DOM structure has been rendered successfully. The second one is called after the DOM structure rendering of the webpage is completed and the resources have been loaded successfully. Is there any difference? One is called when the resource is not loaded, the other is called after the resource loading is complete and the page has been rendered, so when we call $ ('img ') in $ (function '). width (), because the image has not been loaded, the label height is 0, so the return value is 0. However, when you call window. onload = function () {}, the image has been loaded, so the image height can be obtained at this time. So remember, $ (function () {}) is executed when DOM rendering ends and resources are not loaded yet. If you want to get some resource information, there is no way at this time.
Related Article

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.