Chrome JQ Width () method takes a value of 0 solution _jquery

Source: Internet
Author: User
Http://photo.163.com/shixiaojian089/train/28002 This is NetEase's an album, see after want to try to do look.

In my production method, you need to get the width of each photo, so it's natural to use the JQ width () method. It's not a big problem to run under FF and IE, but on Chrome, there's a problem.

Using alert troubleshooting, we found that the chrome width method takes a value of 0. In this case, the picture is not loaded at all when the script is running here. The problem should be in $ (function () {}), because this method only requires the DOM to run when it finishes loading. Then change into the onload under the implementation, sure enough this can be. But this is obviously not a very good way, after all, in the onload run to wait until the entire contents of the whole file loaded and then run the script.

Online search After, found that this brother Mody Http://www.jb51.net/article/50402.htm also encountered the same problem, under its comments there is a solution, you can refer to:

In the place where you want to get the picture wide and high
Copy Code code as follows:

$img. Load (function () {
var img_h = $img. Height ();
var img_w = $img. Width ();
}

This can still use the $ (function () {}), call the Load method on the picture object where the picture is loaded, and avoid waiting for the entire contents of the file to be loaded.

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.