$("#imgDiv img").each(function () { var img = $(this); var img_src = img.attr("src") + "?" + Math.random(); var frameid = "frameimg" + Math.random(); var image = new Image(); image.src = img_src; image.onload = function () { window.img = ‘<img id="img" src=\‘‘ + img_src + ‘\‘/>‘; window.img += ‘<style></style>‘; window.img += ‘<script>window.onload=function(){‘; window.img += ‘parent.document.getElementById(\‘‘ + frameid + ‘\‘).height=document.getElementById(\‘img\‘).height+\‘px\‘‘; window.img += ‘parent.document.getElementById(\‘‘ + frameid + ‘\‘).width=document.getElementById(\‘img\‘).width+\‘px\‘‘; window.img += ‘}<‘ + ‘/script>‘; img.parent().append(‘<iframe width="‘ + image.width + ‘px" height="‘ + image.height + ‘px" id="‘ + frameid + ‘" src="javascript:parent.img" frameBorder="0" scrolling="no"></iframe>‘); img.remove(); } });
此方法每张图片会创建一个iframe,比较耗费资源,请根据实际情况确定是否使用