例如,我有很多图像,所有图像的原始宽度为100px,但原始高度不同。如何根据这些图像的原始高度自动添加高度属性。如如何给img标签添加高度属性?
<img src="picture.jpg" width="100" height="132">
<img src="picture.jpg" width="100" height="45">
<img src="picture.jpg" width="100" heighst="321">
<img src="picture.jpg" width="100" height="136">
<img src="picture.jpg" width="100" height="214">
......
我试试这个,我试图让每一个影像第一的原始高度,
$(document).ready(function() {
$items = $('img');
$.each($items,function(k, v){
console.log(v.naturalHeight);
});
});
它不工作
这个工作有时
$items = $('img');
$.each($items,function(k, v){
$(this).load(function(){
console.log($(this)[0].naturalHeight);
$(this).attr('height', $(this)[0].naturalHeight);
});
});
问题是,在运行脚本之前,如何确保所有图像都已加载?
您的意思是图片的原始尺寸? –
需要更多信息,你会把这个值放在什么基础上?如果您将设置imagem的原始大小,那么您就不需要它了。让它为空,img将假定图像的高度大小,但是如果你想设置其他值,你将如何设置它? – Guerra
你是如何完全不同高度的所有图像,哪一个应该得到多少? –