作者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