我想在每个循环中使用jquery连接图像的html。这是我的代码不起作用。我从li属性获取图像的URL,然后为每个URL设置图像标签。我能够成功获取url,但无法连接每个li的图像标签。需要帮助
$scope.getAgendaReschedules = function (agenda, startTime, endTime, isAgendaNextOrPrevious) {
demoReschedules.getRescheduls($scope.assignedAgentId, "RESCHEDULED", startTime, endTime).then(function (response) {
if (response.status == 200) {
if (response.data.length > 0) {
var groupByScheduledContacts = response.data.reduce(function (result, current) {
result[current.scheduled] = result[current.scheduled] || [];
result[current.scheduled].push(current);
return result;
}, {});
$scope.events = [{}];
for (key in groupByScheduledContacts) {
var eventTime = moment(key, "DD/MM/YYYY HH:mm:ss");
$scope.events.push({
title: 'No. of rescheduled call : ' + groupByScheduledContacts[key].length,
start: eventTime,
end: eventTime,
description: 'This is a cool event',
color: $scope.getColorForReschedules(eventTime)
});
}
$scope.calendarOptions.events = $scope.events;
if (isAgendaNextOrPrevious) {
$scope.calendarOptions.defaultDate = moment(startTime);
$scope.calendarOptions.defaultView = agenda;
}
else {
$scope.calendarOptions.defaultDate = moment(startTime);
$scope.calendarOptions.defaultView = agenda;
}
}
else {
$scope.calendarOptions.events.push({});
$scope.calendarOptions.defaultDate = moment(startTime);
$scope.calendarOptions.defaultView = agenda;
}
}
}, function err() { });
};
答案 0 :(得分:0)
您需要执行gethtml += '<img src="'+img_link+'">';
并将gethtml
初始化为''
,然后在循环之外:
var gethtml ='';
$('.nav li').each(function(){
var img_link = $(this).attr('data-menu');
gethtml += '<img src="'+img_link+'">';
});
$('.main-img').html(gethtml);
答案 1 :(得分:0)
尝试以下代码:
let gethtml = '';
$('.nav li').each(function() {
var img_link = $(this).attr('data-menu');
var img_html = '<img src="'+img_link+'">';
gethtml += img_html + ', ';
});
$('.main-img').html(gethtml);
它将用{em> 分隔的gethtml
变量与img_html
连接起来。
答案 2 :(得分:0)
最好使用append进行如下操作:
$('.nav li').each(function () {
$('.main-img').append($(document.createElement('img')).attr("src", $(this).attr('data-menu')));
});