【记录】JS 获取图片原始尺寸-防止图片溢出

【记录】JS 获取图片原始尺寸-防止图片溢出

示例代码:

<div id="div_content">    <img src="http://static.cnblogs.com/images/logo_small.gif" alt="Alternate Text" /></div><script type="text/javascript">    $(document).ready(function () {        var myimg;        var maxwidth = 643;        $("#div_content img").load(function () {            for (i = 0; i < $(this).length; i++) {                myimg = $(this)[i];                if (myimg.width > maxwidth) {                    myimg.width = maxwidth;                }            }        });    });</script>

参考资料:

  • Javascript获取图片的原始尺寸
  • jquery .each() .load() wait for completion
  • jQuery .load inside .each