我有一个函数'refreshDisplay',这将帮助我创建动态元素。 现在,我的病情很小。 在图像的'src'属性中,我将检查obj.picture.thumbnail是以'/ content'开头,然后将'.jpg'作为'src'属性的扩展名添加,否则什么都不做!更改动态元素的属性,如果它通过条件
请检查代码中类'.myLink'的图像。 我该如何做到这一点?
这是代码。
function refreshDisplay() {
$('.container').html('');
\t savedData.forEach(function (obj) {
\t // Reset container, and append collected data (use jQuery for appending)
\t $('.container').append(
\t \t \t $('<div>').addClass('parent').append(
\t \t \t \t $('<label>').addClass('dataLabel').text('Name: '),
\t \t \t \t obj.name.first + ' ' + obj.name.last,
\t \t \t \t $('<br>'), // line-break between name & pic
\t \t \t \t $('<img>').addClass('myLink').attr('src', obj.picture.thumbnail), $('<br>'),
\t \t \t $('<label>').addClass('dataLabel').text('Date of birth: '),
\t \t \t \t obj.dob, $('<br>'),
\t \t \t \t $('<label>').addClass('dataLabel').text('Address: '), $('<br>'),
\t \t \t \t obj.location.street, $('<br>'),
\t \t \t \t obj.location.city + ' ' + obj.location.postcode, $('<br>'),
\t \t \t \t obj.location.state, $('<br>'),
\t \t \t \t $('<button>').addClass('removeMe').text('Delete'),
\t \t \t \t $('<button>').addClass('top-btn').text('Swap with top'),
\t \t \t \t $('<button>').addClass('down-btn').text('Swap with down')
\t \t \t) \t
\t \t);
\t })
\t // Clear checkboxes:
\t $('.selectRow').prop('checked', false);
\t handleEvents();
}
非常感谢!适用于我,只需要一次更改 - .attr(“src”,function(i,src){/}返回/^\/content/.test(obj.picture.thumbnail)?src +“.jpg”:obj。 picture.thumbnail }) – Sunny