【发布时间】:2014-10-02 17:41:45
【问题描述】:
我正在尝试为网页上没有这些属性的每个图像动态获取和添加宽度、高度和 alt 属性。但它只检测第一个图像宽度高度 alt 属性并将它们添加到所有图像中。
$(document).ready(function() {
var width = $('img').width();
var height = $('img').height();
var image_width = $('img').attr('width');
var image_height = $('img').attr('height');
var image_alt = $('img').attr('alt');
if (typeof image_width === 'undefined' || image_width === false && typeof image_height === 'undefined' || image_height === false) {
$('img').attr('width', width).attr('height', height);
}
if (typeof image_alt === 'undefined' || image_alt === false) {
$('img').attr('alt', 'image');
}
});
【问题讨论】:
标签: javascript jquery