There are some methods that are prone to errors to obtain the size of Dynamically Loaded Images. The main cause of the error is:
You can call the code before the code is downloaded from the web page. This is not easy to find during local development.
The BUG of jQuery load () event processing. When an image is obtained from the browser cache, the error size is obtained.
The error code is:
(Error) Call the code to get the size immediately after HTML is added
Copy codeThe Code is as follows:
Var html = '';
Certificate ('{my_div'{.html (html );
Var width = $ ('# my_div img'). width (); // may return 0
(Error) Use jQuery's load () event for processing
Copy codeThe Code is as follows:
Var html = '';
Var img = $ (html );
Html. load (function (){
// Return 0 if image is loaded from browser cache
Var width = img. width ();
});
('{My_div'{.html (img );
The following is the correct method, using the JavaScript Image class:
Correct Method
Copy codeThe Code is as follows:
Var html = '';
Certificate ('{my_div'{.html (html );
Var ni = new Image ();
Ni. onload = function (){
Var width = ni. width;
}
Ni. src = img. attr (URL );