Home > Web Front-end > JS Tutorial > Solution to the jq width() method value of 0 under chrome_jquery

Solution to the jq width() method value of 0 under chrome_jquery

WBOY
Release: 2016-05-16 16:46:59
Original
1331 people have browsed it

http://photo.163.com/shixiaojian089/train/28002 This is a photo album from NetEase. After seeing it, I wanted to try it.

In my production method, I need to get the width of each photo, so it is natural to use jq’s width() method. There is no big problem when running under FF and IE, but problems arise when running on chrome.

Use alert to troubleshoot and find that the values ​​obtained by the width method under chrome are all 0. In this case, it seems that the image is not loaded at all when the script runs here. The problem should be with $(function(){});, because this method only requires the dom to be loaded before starting to run. Then change it to execute under onload, and sure enough it works now. However, this is obviously not a good idea. After all, running under onload requires waiting until all the contents of the entire file are loaded before running the script.

After searching online, I found this guy http://www.jb51.net/article/50402.htm also encountered the same problem. There is a message below his comment For solutions, please refer to:

Use


$img.load(function(){
var img_h = $img.height();
var img_w = ​​$img.width();
}

In this way, you can still use $(function(){}); to call the load method on the image object where the image needs to be loaded, avoiding waiting for the entire file content to be loaded.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template