There are some very error-prone ways to get the size of dynamically loaded pictures, the main reason for this error is:
You call it before the code is downloaded from the Web page, which is not easy to spot when you develop the computer.
The JQuery load () event handles a bug that gets the wrong size when the image is obtained from the browser cache.
The wrong code is:
(error) calling code to get dimensions immediately after HTML has been added
Copy Code code as follows:
var html = ';
$ (' #my_div '). HTML (HTML);
var width = $ (' #my_div img '). width (); May return 0
(error) handling with a jquery load () event
Copy Code code as follows:
var html = ';
var img = $ (HTML);
Html.load (function () {
Return 0 if image are loaded from browser cache
var width = img.width ();
});
$ (' #my_div '). HTML (IMG);
Here's the really right way to use the JavaScript image class:
The right way
Copy Code code as follows:
var html = ';
$ (' #my_div '). HTML (HTML);
var ni = new Image ();
Ni.onload = function () {
var width = ni.width;
}
NI.SRC = Img.attr (URL);