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

时间:2021-07-08 06:03:35

示例代码:

<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>

参考资料: