javascript - Works in Console but not in .js file -
i trying add width , height attributes images; jquery used works in console not in linked .js file. have tried , doesn't work.
$(document).ready(function () { $('.media-box-image').each(function () { var $width = $('.media-box-image').width(); var $height = $('.media-box-image').height(); var $this = $(this).find('img'); $this.attr('width', $width); $this.attr('height', $height); }); });
i tried didn't work
$(document).ready(function () { $('.media-box-image').each().on('load', function () { var $width = $('.media-box-image').width(); var $height = $('.media-box-image').height(); var $this = $(this).find('img'); $this.attr('width', $width); $this.attr('height', $height); }); });
i checked console seems fine. else can do?
when document ready callback executes during initial page load cycle, page not have finished loading image data.
because there's no image data, images have neither widths nor heights.
use $(window).on('load'...)
instead of $(document).ready(...)
Comments
Post a Comment