作者Fantasywind (Fantasywind)
看板Ajax
标题[问题] jQuery width()
时间Wed Jul 6 22:39:28 2011
$("img.textPic").each(function(){
var max_w = parseInt($(this).parent().css('width').replace("px",'')) - 30;
var max_h = parseInt($(this).parent().css('height').replace("px",'')) - 250;
var div = max_w / max_h;
var img = $(this).width() / $(this).height();
if (div > img){
$(this).width(max_w);
} else {
$(this).height(max_h);
}
})
以上在$(this).width() 取到的值总是0
img的标签是用.append()动态产生并附上class
且较上段程式码先执行
父元素的div标签也已同样动态方式产生却可以正确取到css值
想请问是哪里出了问题导致.width() 无法取img标签的图片大小呢?
each改成load也无法取
问题应该是出在img来不及载入就跑完js
在img tag 必须是动态操作的情况下有没有比较好的解法?
还是只能靠php抓大小来喂了QQ
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 58.114.242.134
※ 编辑: Fantasywind 来自: 58.114.242.134 (07/06 22:40)
※ 编辑: Fantasywind 来自: 58.114.242.134 (07/06 23:15)
1F:推 senser:timing issue 可以试试setTimeout 07/18 02:31