jquery怎样获取DIV里多个图片的尺寸? jQueryHTML图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 jQuery(document).ready(function(){ $('img').each(function(){ //jquery.each()循环读取所有图片 var height = $(this).height(); var width = $(this).width(); if(width>200){ $(this).css('height',Math.round(height*200/width)+'px');//如果宽度超过200px,高度等比例缩放 } });}); 如果图片宽高被css修改过,一楼获取到的可能就不一定是图片的真正尺寸了。要获取图片的真实尺寸,可以尝试利用Image类重新加载一遍图片:var img = new Image();img.onload = function(){ console.log("width:"+img.width); console.log("height:"+img.height);};img.onerror = function(){ console.log("图片获取失败!");};var timer = setInterval(function(){ if(!!img.width){ clearInterval(timer); console.log("width:"+img.width); console.log("height:"+img.height); }},500);img.src="[图片地址]"; 额.我用PHP搞掂了.不过JQUERY貌似不能选择PHP动态生成的图片呢..哎 求一个Javascript函数(控件小数点位数并实现非零进一) 求救啊!!JS为什么自动调用后台方法啊! extjs TabPanel 中打开tab1和tab2出现混乱 求助 jquery在IE中使用append没有反映? 字符串里空格 关于showModalDialog的问题 js函数中 如何阻止事件冒泡 求"设置IE的页面设置的上下左右页边距脚本" 有很多对象的属性都不知道是干嘛用的,请大家推荐本手册什么的,感激不尽 js文件在frame中的应用问题。搞定就给分。 请教个关于js面向对象的问题 急!!请帮帮忙解决这段代码在IE不兼容的问题
$('img').each(function(){ //jquery.each()循环读取所有图片
var height = $(this).height();
var width = $(this).width();
if(width>200){
$(this).css('height',Math.round(height*200/width)+'px');//如果宽度超过200px,高度等比例缩放
}
});
});
var img = new Image();
img.onload = function(){
console.log("width:"+img.width);
console.log("height:"+img.height);
};
img.onerror = function(){
console.log("图片获取失败!");
};
var timer = setInterval(function(){
if(!!img.width){
clearInterval(timer);
console.log("width:"+img.width);
console.log("height:"+img.height);
}
},500);
img.src="[图片地址]";