jquery中如何获取图片真实大小

互联网 20-11-18

jquery中获取图片真实大小的方法:1、方法一【.attr("src", $(''#imgid").attr("src"))】;2、方法二【theImage.src = $(''#imgid").attr( "src");】。

本教程操作环境:windows10系统、jquery2.2.4,本文适用于所有品牌的电脑。

jquery中获取图片真实大小的方法:

第一种:

$("<img/>") // Make in memory copy of image to avoid css issues  在内存中创建一个img标记     .attr("src", $(''#imgid").attr("src"))     .load(function() {         pic_real_width = this.width;   // Note: $(this).width() will not         pic_real_height = this.height; // work for in memory images.     });

第二种:

var theImage = new Image();  theImage.src = $(''#imgid").attr( "src");  alert( "Width: " + theImage.width);  alert( "Height: " + theImage.height);

第一种要安全性要高些,保证是在图片加载完成后在获取。

相关免费学习推荐:JavaScript(视频)

以上就是jquery中如何获取图片真实大小的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: 图片大小
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:怎么使用下载的jquery插件

相关资讯