如何用js获取html内图片的宽高并写入
标签内 多个图片自动获取怎么写
单个是这个写的:

function getImageSize(imgE){ var i = new Image(); //新建一个图片对象 i.src = imgE.src;//将图片的src属性赋值给新建的图片对象的src return new Array(i.width,i.height); } var imageE = document.getElementById("imageE"), imageSize = getImageSize(imageE), imgwidth = imageSize[0], imgheight = imageSize[1]; $("#imageE").attr("width",imgwidth); $("#imageE").attr("height",imgheight); console.log(imgwidth); console.log(imgheight);
但是这个只能判断一个,那如果id是变量的如何写呢?
首先这些图片是已经存在的么?
如果是已经存在的图片就简单了。
Jquer的写法:
$('img').each((i,v)=>{
let img_w = $(v).width();
let img_h = $(v).height();
$(v).attr({width:img_w,height:img_h});
});
有些es6的特性语法,你应该认识的。
var w= $(img).width();
var h = $(img).height();
$(img).css({"width";w,"height":h});