The $(document).ready function fires when the DOM has been fully built, so
not all the images will be loaded at that time.
You could bind to the images load method as well.
function fixDimensions()
{
var img = $(this);
img.attr('width',img.width());
img.attr('height',img.height());
}
$('img').each(fixDimensions).bind('load',fixDimensions);
JK
-----Original Message-----
From: jquery-en@googlegroups.com [mailto:jquery-en@googlegroups.com] On
Behalf Of enchance
Sent: Friday, November 28, 2008 5:52 PM
To: jQuery (English)
Subject: [jQuery] Adding IMG attributes
I'm trying to add several attributes to all my img tags but I'm not
sure if they're both working. Could you verify if these are correct?
$(document).ready(function(){
//add the width and height for each image
$('img').each(function(){
$(this).attr('width', $(this).width());
$(this).attr('height', $(this).height());
});
//copy alt to title
$('img').attr('title', function(){
$(this).attr('alt');
});
});
Am I doing the right thing?
No comments:
Post a Comment