Solution for setting the jqwidth () method to 0 in chrome _ jquery

Source: Internet
Author: User
When using the jq width () method in ff and ie running problem is not big, but to chrome, there is a problem http://photo.163.com/shixiaojian089/train/28002 this is a Netease album, after seeing want to try to do it.

In my production method, I need to get the width of each photo, so I naturally use the jq width () method. It is not a problem to run in ff and ie, but the problem occurs in chrome.

Using alert for troubleshooting, we found that the value obtained by the width method in chrome is 0. In this case, the image is not loaded when the script runs here. The problem should be solved on $ (function () {});, because this method only requires the dom to be loaded to start running. So it can be executed under onload. However, this is obviously not a good method. After all, running in onload will wait until all the content of the entire file is loaded before running the script.

After searching online, I found this guy:

To obtain the image width, use

The Code is as follows:


$ Img. load (function (){
Var img_h = $ img. height ();
Var img_w = $ img. width ();
}


In this way, you can still use $ (function () {}); call the load method for the image object where the image is to be loaded to avoid waiting until the entire file content is 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.