这不考虑替代或错误情况。如果不存在alt属性,则主图像将包含上一图像的alt文本。我想通过给该属性分配一个空字符串来清除该替代文本,例如, attr.alt = (not_undefined) ? thumb.attr() : '';
如何将空字符串分配给属性
我该怎么做?
$(function() {
$('.component-individual-detail-profile').each(function() {
var imgContainer = $(this).find('.photos');
imgContainer.find('li').click(function(e) {
e.preventDefault();
var thumb = $(this).find('img');
var attributes = {
src: thumb.attr('src')
};
if (typeof thumb.attr('alt') !== 'undefined') {
attributes.alt = thumb.attr('alt');
}
if (typeof thumb.attr('title') !== 'undefined') {
attributes.title = thumb.attr('title');
}
imgContainer.find('.selected').attr(attributes);
});
});
});
只需声明你的属性变量就像这样:var attributes = {src:thumb.attr('src'),alt:'',title:''};'。 –